0

我有一个列表,SelectedListItem我将它从控制器发送到使用 ViewBag 进行查看。List中有一个具有selected = true属性的 SelectedListItem。

鉴于代码是:

@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}
4

1 回答 1

1

将您选择的值设置为model.Configuration属性。

DropDownListFor仅当您的属性为type 且当前为时才适用于selected = true元素,否则从属性值中设置选定的值。ConfigurationNullablenullDropDownListForConfiguration

编辑:

selectListItem应该是value = "Red" and text = "Red"

或者

Model.Configuration = "Color1"

DropDownListFor设置值取决于Value属性,而不是Text

于 2015-02-07T13:13:22.157 回答