型號的HTML:
public class MyViewModel
{
public IList<AdminSummary> AdminSummaries { get; set; }
}
public class AdminSummary
{
public string Status { get; set; }
}
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptions()
{
return new[]
{
new SelectListItem { Value = "1", Text = "status 1" },
new SelectListItem { Value = "2", Text = "status 2" },
new SelectListItem { Value = "3", Text = "status 3" },
};
}
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
AdminSummaries = new[]
{
// preselect the first item
new AdminSummary { Status = "1" },
// preselect the second item
new AdminSummary { Status = "2" },
// nothing will be preselected because there is no xxx Value in the list
new AdminSummary { Status = "xxx" },
}.ToList()
};
return View(model);
}
}
查看:
@model MyViewModel
@for (int index = 0; index < Model.AdminSummaries.Count; index++)
{
@Html.DropDownListFor(
x => x.AdminSummaries[index].Status,
new SelectList(
AdminStatusReference.GetAdminStatusOptions(),
"Value",
"Text",
Model.AdminSummaries[index].Status
)
)
}
列表是一個IEnumerable,所以我不認爲這是你的問題 – 2011-12-29 16:25:37
你是什麼意思,通過正確選擇正確的值? 你的意思是默認的選定值,或者當你選擇一個項目併發布表單時,selectedItem是錯誤的? – 2011-12-29 16:27:05
你有什麼問題?它與List無關IEnumerable無關。 – gdoron 2011-12-29 16:27:51