ASP.NET MVC bug绑定DropDownList的集合?

Evg*_*nyt 5 collections asp.net-mvc

我有一个视图,其中包含从Model属性生成的1个下拉列表以及从数组属性生成的3个附加下拉列表

@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
@for (int i = 0; i < Model.AgentTypes.Length; i++)
{
    @Html.DropDownListFor(m => m.AgentTypes[i], Model.AgentTypeListItems)
}
Run Code Online (Sandbox Code Playgroud)

控制器方法初始化AgentTypeListItems集合+设置AgentType下拉列表的默认值和集合的3个下拉列表:

var model = new OptionsViewModel();

// for DropDownListFor
model.AgentTypeListItems = new[]
{
    new SelectListItem { Text = "1", Value = "1" }, 
    new SelectListItem { Text = "2", Value = "2" },
    new SelectListItem { Text = "3", Value = "3" },
};

// 1 dropdown in the model
model.AgentType = "2";

// 3 dropdowns in array
model.AgentTypes = new[] { "3", "2", "1" };

return View(model);
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中打开它时,尽管AgentTypes数组初始化为不同的值(!),但我到处都是"2":

这是错的

当我用TextBoxFor替换DropDownListFor时:

@Html.TextBoxFor(m => m.AgentTypes[i])
Run Code Online (Sandbox Code Playgroud)

我在输入中得到了正确的值(!):

这应该是怎么回事

这意味着TextBoxFor按预期工作,但DropDownListFor没有.

这是MVC DropDownListFor中的错误吗?

更新 这是模型类:

public class OptionsViewModel
{
    public SelectListItem[] AgentTypeListItems { get; set; }
    public string AgentType { get; set; }
    public string[] AgentTypes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*kov 14

我知道这篇文章已有一年多的历史,但它似乎仍然是一个错误.更换

@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
@for (int i = 0; i < Model.AgentTypes.Length; i++)
{
    @Html.DropDownListFor(m => m.AgentTypes[i], Model.AgentTypeListItems)
}
Run Code Online (Sandbox Code Playgroud)

@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
@for (int i = 0; i < Model.AgentTypes.Length; i++)
{
    @Html.DropDownListFor(m => m.AgentTypes[i], new SelectList(Model.AgentTypeListItems, "Value", "Text", Model.AgentTypes[i])
}
Run Code Online (Sandbox Code Playgroud)

适合我.