简体   繁体   中英

Can I use an ArrayList as a SelectList in ASP.NET MVC

I am creating a ASP.NET Web Application as part of my studies.

At the moment I am creating an Add Product 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:

EDIT: As someone pointed out below, ArrayList is no longer recommended. That's part of why I am trying to use this method.

    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();

    }

This is then used on page load.

This works fine when I'm creating it using web forms. However, I would like to use the @Html.DropDownList action link for this project. When connecting databases using scaffolding these dropdownlists are created and populated just fine, and I can see where the SelectList is generated for the view, ie:

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

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:

     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>;

But that doesn't seem right.

EDIT: As pointed out below, I need to add the Value to the new SelectListItem :

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

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?

ADDED QUESTION: Also, how should I add this new list to the ViewBag ?:

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

My problem at the moment is it is within the GetImages method, and I am unsure how access it. I think the answer is super basic but I'm very new to this.

Any advice would be much appreciated!

Thanks again.

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM