2016-07-27 7 views
0

私は研究の一部としてASP.NET Webアプリケーションを作成しています。ASP.NET MVCのSelectListとしてArrayListを使用できますか

現時点では、を追加しています。セクションを追加してください。いくつかの画像を画像フォルダに追加しました。これらの画像名をドロップダウンリストに追加したいと思います。これは私のチュートリアルで提供されたコードです:

編集:誰かが指摘したように、ArrayListはもはや推奨されていません。それが私がこの方法を使用しようとしている理由の一部です。

public void GetImages() 
    { 
     try 
     { 
      //get all filepaths 
      string[] images = Directory.GetFiles(Server.MapPath("~/Images/Products/")); 

      //get all filenames and add them to an arraylist. 

      ArrayList imagelist = new ArrayList(); 
      foreach (string image in images) 
      { 
       string imagename = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1); 
       imagelist.Add(imagename); 
      } 

     //Set the arrayList as the dropdownview's datasource and refresh 
     ddlImage.DataSource = imageList; 
     ddlImage.AppendDataBoundItems = true; 
     ddlImage.DataBind(); 

    } 

これはページの読み込みに使用されます。

これは、Webフォームを使用して作成するときにうまく動作します。ただし、このプロジェクトには@Html.DropDownListアクションリンクを使用したいと思います。これらdropdownlistsを作成し、うまく移入され足場を使用してデータベースに接続する、と私はSelectListのはすなわち、ビューのために生成される場所を確認できた場合:

// GET: Products/Create 
    public ActionResult Create() 
    { 
     ViewBag.TypeId = new SelectList(db.ProductTypes, "Id", "Name"); 
     return View(); 
    } 

私はちょうどに私のチュートリアルの例を有効にする方法を確認していませんSelecListイニシャライザが呼び出すIEnumerable。私が得た最も近いものは次のとおりです。

 List<SelectListItem> imagelist = new List<SelectListItem>(); 
      foreach (string image in images) 
      { 
       string imagename = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1); 
       imagelist.Add(new SelectListItem() { Text = imagename }); 
      } 

      IEnumerable<string> imager = imagelist as IEnumerable<string>; 

しかし、それは正しいようです。

EDIT:以下指摘したように、私は新しいSelectListItemに値を追加する必要があります。

 imagelist.Add(new SelectListItem() { Text = imagename, Value = "Id" }); 

これは良さそうです。私は "イメージャ"を作成する必要があるかどうかはわかりませんが、imageListはIEnumerableとして作成します。 SelectListはすでに列挙できませんか?

を追加しましたQUESTION:また 、私はViewBagに、この新しいリストを追加する方法:現時点では

ViewBag.TypeId = new SelectList(db.ProductTypes, "Id", "Name"); 
    ViewBag.TypeId = new SelectList() 
    return View();  

私の問題は、それがGetImagesメソッド内であり、そして私はどのようにアクセスそれがわからないのですか?。答えは基本的なものだと思いますが、これはとても新しいものです。

アドバイスをいただければ幸いです!

もう一度おねがいします。

+2

期間、ArrayListをもう使用しないでください。なぜそのコードは正しいと思わないのですか? SelectListItemに値を設定する必要があります。 – CodeCaster

+0

'string imagename = image.Substring(image.LastIndexOf(@" \ "、StringComparison.Ordinal)+ 1)' '文字列を使用するimagename = Path.GetFileName(image)'(https://msdn.microsoft.com /ru-ru/library/system.io.path.getfilename(v=vs.110).aspx) – feeeper

+0

私はそこに着いています。私は値の設定を理解しています。今私はそれをViewBagに追加する必要があります。どのように私はそれを書くだろうか? ViewBag.Image = new SelectList(?) – ScottCoding

答えて

0
//Create a new select list. the variable Imagelist will take on whatever type SelectList. 

    var Imagelist = new SelectList(
    new List<SelectListItem> 
    { 
     new SelectListItem { Text = imagename, Value = "Id"}, 
     new SelectListItem { Text = imagename2, Value = "Id2"}, 

    }, "Value" , "Text"); 


    //You can now use this viewbag in your view however you want. 
     ViewBag.Image = Imagelist.