1

如何在模型对象数组中设置 Razor 中 DropdownList 的选定值?例子:

@foreach (var model in Model.Courses)
{
   ....
   <td>
       @Html.DropDownList(**** set the selected value from model.Order, from my Model.Positions list ****)
   </td>
   ....
}

谢谢!

4

2 回答 2

4

尝试这个

@foreach (var model in Model.Courses)
{
   ....
   <td>
                          //Control Name,  Options,     Selected Value
       @Html.DropDownList("NameOfControl", ListOfItems, model.Value)
   </td>
   ....
}
于 2013-05-01T22:25:42.200 回答
1

最好尝试使用 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"]) )
于 2013-05-02T10:15:47.500 回答