title: Can I use an ArrayList as a SelectList in ASP.NET MVC
body: <p>I am creating a ASP.NET Web Application as part of my studies. </p>
  
  <p>At the moment I am creating an <strong>Add Product</strong> section. I have added some images to an image folder, and would like those image names to be added to a dropdownlist. This is the code my tutorial offered:</p>
  
  <p>EDIT: As someone pointed out below, ArrayList is no longer recommended. That's part of why I am trying to use this method. </p>
  
  <pre><code>    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();
  
      }
  </code></pre>
  
  <p>This is then used on page load. </p>
  
  <p>This works fine when I'm creating it using web forms. However, I would like to use the <code>@Html.DropDownList</code> action link for this project. When connecting databases using scaffolding these dropdownlists are created and populated just fine, and I can see where the <strong>SelectList</strong> is generated for the view, ie:</p>
  
  <pre><code>    // GET: Products/Create
      public ActionResult Create()
      {
          ViewBag.TypeId = new SelectList(db.ProductTypes, "Id", "Name");
          return View();
      }
  </code></pre>
  
  <p>I am just not sure how to turn my tutorial example into the IEnumerable, which the SelecList initializer calls for. The closest I've gotten is this:</p>
  
  <pre><code>     List&lt;SelectListItem&gt; imagelist = new List&lt;SelectListItem&gt;();
              foreach (string image in images)
              {
                  string imagename = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
                  imagelist.Add(new SelectListItem() { Text = imagename });
              }
  
               IEnumerable&lt;string&gt; imager = imagelist as IEnumerable&lt;string&gt;;
  </code></pre>
  
  <p>But that doesn't seem right.</p>
  
  <p>EDIT: As pointed out below, I need to add the Value to the new <code>SelectListItem</code>:</p>
  
  <pre><code>     imagelist.Add(new SelectListItem() { Text = imagename, Value = "Id" });
  </code></pre>
  
  <p>This seems better. Although I am not sure whether I need to create "imager", the imageList as an IEnumerable. Isn't a SelectList already Enumerable? </p>
  
  <p>ADDED QUESTION:
  Also, how should I add this new list to the <code>ViewBag</code>?:</p>
  
  <pre><code>    ViewBag.TypeId = new SelectList(db.ProductTypes, "Id", "Name");
      ViewBag.TypeId = new SelectList()
      return View();     
  </code></pre>
  
  <p>My problem at the moment is it is within the <code>GetImages</code> method, and I am unsure how access it. I think the answer is super basic but I'm very new to this. </p>
  
  <p>Any advice would be much appreciated! </p>
  
  <p>Thanks again. </p>