我尝试在我的视图中填充DropDownListFor,但我似乎并没有得到它的工作。填充DropDownListFor与初始值
我的模型
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 { }
}
}
在我的ViewPage我有以下代码
@model IEnumerable<MyApp.Models.Warehouse>
<!-- some html -->
@foreach(var s in Model)
{
@Html.DropDownListFor(p => s.LocationCode, s.LocationList, "", new { @class = "form-control" })
}
我想通过设置p => s.LocationCode
我设置的下拉列表中初始选定值(选择的值应该是空的,如果s.LocationCode == null
),但我的结果是所有的下拉框都有一个初始的空值(当我点击其中的一个时,你会看到下拉列表被填充)。
有在SO太多类似问题。看看这个,例如:http://stackoverflow.com/questions/16034454/create-many-dropdownlistfor-in-foreach-loop – ataravati