I try to populate a DropDownListFor in my view, but I don't seem to get it working.
My Model
public class Warehouse
{
public int Id { get; set; }
public string Description { get; set; }
public string LocationCode { get; set; }
public IEnumerable<SelectListItem> LocationList
{
get
{
DbEntities db = new DbEntities();
var List = new SelectList(db.Locations, "LocID".Trim(), "Desc".Trim());
return List;
}
set { }
}
}
In my ViewPage I have the following code
@model IEnumerable<MyApp.Models.Warehouse>
<!-- some html -->
@foreach(var s in Model)
{
@Html.DropDownListFor(p => s.LocationCode, s.LocationList, "", new { @class = "form-control" })
}
I thought by setting p => s.LocationCode
I set the initial selected value for the dropdownlist (the selected value should be empty if s.LocationCode == null
), but my result is that all drop-down boxes are have an initial empty value (and when I click on one of them, you see that the dropdownlist is populated).