我嘗試在我的視圖中填充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