我有一个SelectedListItem列表,我将它从控制器发送到使用ViewBag查看。在列表中有一个带有selected = true属性的selected = true。
考虑到该守则是:
@Html.DropDownListFor(model => model.Configuration,
new SelectList(ViewBag.ListOfConfigValues,"Value", "Text"),
new { @class = "ddl", @id = "ddlForConfiguration"})我还传递希望在model.Configuration中显示的值。
我知道我们可以将所选的索引与Value、Text一起传递。
在控制器中,我将ViewBag设置为
ViewBag.ListOfConfigValues = defaultObj.configurationsList;
return PartialView(detail);configurationList如下所示
{Text = "Red", Value = "Color1", Selected = false},
{Text = "Blue", Value = "Color2", Selected = true},
{Text = "Green", Value = "Color3", Selected = false}发布于 2015-02-07 13:13:22
将选定的值设置为model.Configuration属性。
只有当您的DropDownListFor属性为Nullable类型并为null时,才能使用Nullable元素,否则DropDownListFor将从Configuration属性值中设置选定的值。
编辑:
你的selectListItem应该是value = "Red" and text = "Red"
或
Model.Configuration = "Color1"DropDownListFor集值取决于Value属性,而不是Text
https://stackoverflow.com/questions/28382434
复制相似问题