0

Right now, I'm trying to create an edit page for an MVC application that utilizes a drop-down list, allowing the user to edit an employee's location. I consider two scenarios: one where the employee has no set location, and one in which the employee already has an assigned location. In the second case, I would like the drop down list in the edit page to automatically have the current location selected in the list. The list is successfully created, however it defaults to the first value (in this case the empty string) in the SelectList, NOT the currently selected value. The controller code for this case is as follows:

var pairs = db.Locations.Select(x => new { value = x.LocationID, text = x.City }).ToList();
            pairs.Insert(0, (new {value = 0, text = ""}));
            SelectList Locations = new SelectList(pairs,
            "value", "text", pairs.First(x=> x.value == employee.Location.LocationID));
            foreach (SelectListItem item in Locations)
            {
                item.Selected = false;
            }
            foreach (SelectListItem item in Locations)
            {
                if (item.Value == (employee.Location.LocationID.ToString()))
                {
                    Debug.Print("Match Found");
                    item.Selected = true;
                    break;
                }

            }

            ViewBag.Locations = Locations;

Note that right now, I am explicitly enumerating over the list, and flagging the desired value as selected. Originally, I used the overload for the SelectList constructor that took a "selectedValue" parameter, however this did not work either. Also note the print statement: when running, that line is indeed printed, meaning that the matched value was found and flagged. It simply does not display as such on the page.

The code in the view is as follows:

<div class="form-group">
        @Html.Label("Location", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("location", ViewBag.Locations as IEnumerable<SelectListItem>)
        </div>
    </div>

Is there something that I'm missing?

Thanks!

Tom Kreamer
  • 113
  • 1
  • 2
  • 12
  • 1
    maybe this answer can help you http://stackoverflow.com/questions/27297294/how-to-set-the-default-value-for-html-dropdownlistfor-in-mvc – Daniel Gpe Reyes Aug 26 '15 at 17:46
  • Have you tried just doing: @Html.DropDownList("location", ViewBag.Locations) – Andy Poquette Aug 26 '15 at 18:31
  • Are you using a model? Does the model have a field called "location"? If so, that could cause this issue. – stephen.vakil Aug 26 '15 at 18:38
  • The model has a field called "Location" but it is with an uppercase L. – Tom Kreamer Aug 26 '15 at 18:52
  • you try `@Html.DropDownListFor(model => model.Location, ViewBag.Locations as IEnumerable)`? – JamieD77 Aug 26 '15 at 18:56
  • wait `Locations` is a SelectList in your controller? why is it being cast as `IEnumerable` in your view? – JamieD77 Aug 26 '15 at 19:00
  • You need to show the model your binding to. From your (sorry, but awful) attempt to create a `SelectList` it appears your model may have a property named `Location` which is a complex object and you cannot bind to a complex object. –  Aug 26 '15 at 22:47

1 Answers1

1

I'm pretty sure you can simplify your code greatly and get the same result you're wanting.

You can use this in your controller

var Locations = new SelectList(db.Locations, "LocationID", "City");
ViewBag.Locations = Locations;

And in your view just use DropDownListFor

@Html.DropDownListFor(m => m.Location.LocationID, (SelectList)ViewBag.Locations)
JamieD77
  • 13,796
  • 1
  • 17
  • 27
  • No need for the last parameter in the `SelectList` constructor (its ignored when binding to a property) - but if `Location` is a complex property containing `LocationID` then it needs to be `@Html.DropDownListFor(m => m.Location.LocationID, (SelectList)ViewBag.Locations)` - OP has not shown the model so can't be sure –  Aug 26 '15 at 22:48