0

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?

Analytic Lunatic
  • 3,853
  • 22
  • 78
  • 120
  • 1
    if you had a `ViewModel` with `IEnumerable`, you could use `Html.DropDownListFor()` and add the "Add New" item to that, see here https://msdn.microsoft.com/en-us/library/ee703561(v=vs.118).aspx and here http://stackoverflow.com/questions/20380994/convert-list-to-ienumerableselectlistitem/20381108#20381108 for details – Ric Feb 10 '15 at 16:52
  • Is it not possible to just do so using the `SelectList()`? Why is the `DropDownList()` seeming to be more popularly suggested on this issue? – Analytic Lunatic Feb 10 '15 at 16:58
  • did you resolve this issue? – Ric Feb 13 '15 at 16:28
  • Sort of, but not quite. I ended up adding a [CREATE NEW] button next to each dropdown list for a better user experience (I did end up using a `DropDownListFor()`. The button opens (shows) a hidden form where user can enter (Ex.) a new Location, which is correctly stored in my variables of my script, but is NOT getting passed into my controller action for some reason: https://stackoverflow.com/questions/28503811/javascript-variable-storing-garbage-instead-of-value-entered-into-input-box?noredirect=1#comment45327119_28503811 – Analytic Lunatic Feb 13 '15 at 16:31

1 Answers1

1

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.

Ric
  • 12,855
  • 3
  • 30
  • 36
  • Thanks for helping Ric. I ended up using the `DropDownListFor()`, but for a better user experience decided to just use a `[CREATE NEW]` button to the right of each dropdown. I'm still having issues getting the newly entered value to be passed into my controller and saved to the DB: https://stackoverflow.com/questions/28503811/javascript-variable-storing-garbage-instead-of-value-entered-into-input-box?noredirect=1#comment45327119_28503811 – Analytic Lunatic Feb 13 '15 at 16:36
  • 1
    looking at that now, just wondering about the data types you are passing as it is a string and not an json object per se – Ric Feb 13 '15 at 16:39