看起来您正在尝试设置DropDownList
助手中的SelectList
的SelectedValue
,但这是不可能的。
您必须在创建SelectList
时设置SelectedValue
属性。请参见以下示例。
ViewBag.DropList = new SelectList(new[]{
new SelectListItem{ Text="one", Value="1"},
new SelectListItem{ Text="two", Value="2"},
new SelectListItem{ Text="three", Value="3"}
}, "Value", "Text", 2);
在以上代码中,我已经在初始化
SelectList
时将2设置为选定项目。之后,您可以按照以下方式传递
SelectList
和HTML属性。
@Html.DropDownList("destination", (SelectList)ViewBag.DropList, new { @class = "form-control" })
感谢您的选择!
解决方案:
@Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() {Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination)}), new{@class="form-control"})
DropDownList
是否适用于CustomTypes
?parameters.Destination
是否包含所选值? - Saranga