ASP.NET MVC bug 绑定 DropDownList 集合?
我有一个视图,其中包含从 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)
}
控制器方法初始化 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);
当我在浏览器中打开它时,我得到“ 2" 尽管 AgentTypes 数组使用不同的值初始化(!):
当我用 TextBoxFor 替换 DropDownListFor 时:
@Html.TextBoxFor(m => m.AgentTypes[i])
I在输入中获取正确的值(!):
这意味着 TextBoxFor 按预期工作,但是DropDownListFor 没有。
这是 MVC DropDownListFor 中的错误吗?
更新 这是模型类:
public class OptionsViewModel
{
public SelectListItem[] AgentTypeListItems { get; set; }
public string AgentType { get; set; }
public string[] AgentTypes { get; set; }
}
I have a view with 1 dropdown generated from Model property and 3 additional dropdowns that are generated from array property
@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
@for (int i = 0; i < Model.AgentTypes.Length; i++)
{
@Html.DropDownListFor(m => m.AgentTypes[i], Model.AgentTypeListItems)
}
The controller method initializes AgentTypeListItems collection + sets default values for AgentType dropdown and 3 dropdowns for the collection:
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);
When I open it in browser I get "2" everywhere though AgentTypes array was initialized with different values(!):
When I replace DropDownListFor with TextBoxFor:
@Html.TextBoxFor(m => m.AgentTypes[i])
I get the correct values in the inputs (!):
It means the TextBoxFor works as expected, but DropDownListFor doesn't.
Is it a bug in MVC DropDownListFor?
UPDATE
Here is the model class:
public class OptionsViewModel
{
public SelectListItem[] AgentTypeListItems { get; set; }
public string AgentType { get; set; }
public string[] AgentTypes { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这篇文章已经有一年多了,但它似乎仍然是一个错误。替换
为
适合我的作品。
I know that this post is more than a year old but it seems still to be a bug. Replacing
with
works for me.
注释掉你的第一行,
@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
然后会发生什么?
试试这个:
comment out your first line,
@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
what happens then?
Try this:
我想知道这是否类似于 WPF 和 Windows 窗体中的情况,在列表选择中使用相同的源项目在幕后将所有 UI 元素“链接”在一起?我记得在这些情况下必须创建列表对象的单独副本。这里可能值得一试。
I wonder if this is similar to situations in WPF and Windows Forms where using the same source item for the list choices "links" all the UI elements together behind the scenes? I recall having to create separate copies of the list object in those situations. Might be worth a try here.