如何在模型对象数组中设置 Razor 中 DropdownList 的选定值?例子:
@foreach (var model in Model.Courses)
{
....
<td>
@Html.DropDownList(**** set the selected value from model.Order, from my Model.Positions list ****)
</td>
....
}
谢谢!
如何在模型对象数组中设置 Razor 中 DropdownList 的选定值?例子:
@foreach (var model in Model.Courses)
{
....
<td>
@Html.DropDownList(**** set the selected value from model.Order, from my Model.Positions list ****)
</td>
....
}
谢谢!
尝试这个
@foreach (var model in Model.Courses)
{
....
<td>
//Control Name, Options, Selected Value
@Html.DropDownList("NameOfControl", ListOfItems, model.Value)
</td>
....
}
最好尝试使用 SelectList 在数据源本身中设置选定选项
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("Value1", "1");
list.Add("Value2", "2");
list.Add("Value3", "3");
var selectList = new SelectList(list,
"Value", "Key",
"2"); // selected item's value "Value2" is selected.
ViewData["SelectedValue"] = selectList;
@Html.DropDownList("ddlValues", (SelectList)ViewData["SelectedValue"]) )