Search code examples
c#asp.net-mvcrazorviewbagselectlist

MVC5: Insert new value into populated SelectList()?


In my main INV_Assets controller of my MVC5 app, I have an method for Edit() which passes several populated SelectLists() via the ViewBag to allow users to select from all available entities for the relevant list in the other tables of my Database -- Note, if there is a better practice than passing this through the ViewBag, please feel free to guide me to a better way.

        // GET: INV_Assets/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            INV_Assets iNV_Assets = await db.INV_Assets.FindAsync(id);
            if (iNV_Assets == null)
            {
                return HttpNotFound();
            }
            ViewBag.Location_Id = new SelectList(db.INV_Locations, "Id", "location_dept", iNV_Assets.Location_Id);
            ViewBag.Manufacturer_Id = new SelectList(db.INV_Manufacturers, "Id", "manufacturer_description", iNV_Assets.Manufacturer_Id);
            ViewBag.Model_Id = new SelectList(db.INV_Models, "Id", "model_description", iNV_Assets.Model_Id);
            ViewBag.Status_Id = new SelectList(db.INV_Statuses, "Id", "status_description", iNV_Assets.Status_Id);
            ViewBag.Type_Id = new SelectList(db.INV_Types, "Id", "type_description", iNV_Assets.Type_Id);
            ViewBag.Vendor_Id = new SelectList(db.INV_Vendors, "Id", "vendor_name", iNV_Assets.Vendor_Id);
            return View(iNV_Assets);
        }

My lists currently populate fine, but for ease of use I want to insert a value of "Add New" into each list, that when clicked will open pop-up (partial view?) of my relevant Create() View for the relevant entity. For example, if the Locations SelectList() has "Add New" clicked, I want to open my Create view for Locations.

Can anyone offer an example of how to do this?

I've been looking for how to insert a new value into the SelectList() but most of what I seem to be coming across is using the example of forgoing the SelectList() instead for an Html.DropDownList(), though I'm not sure why?


Solution

  • The SelectList class inherits IEnumerable<SelectListItem>, which are used to populate drop down lists.

    Given a ViewModel object that has the following property:

    public SelectList Options
    {
        get
            {
                var items = Enumerable.Range(0, 100).Select((value, index) => new { value, index });
                SelectList s = new SelectList(items, "index", "value");
                return s;
            }
     }
    
    public int SelectedOption { get; set; }
    

    The view:

    @Html.DropDownListFor(m => m.SelectedOption, Model.Options, "Add New", new { @class = "form-control" })
    

    In order to do what you want regarding the popup, you probably need some javascript to deal with this.

    If you don't want the "Add New" in the DropDownListFor() you would need to add it manually to your collection before returning it to the view.

    Hope this helps.