ASP.NET MVC bug 绑定 DropDownList 集合?

发布于 2024-10-26 11:58:07 字数 1470 浏览 2 评论 0原文

我有一个视图,其中包含从 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 数组使用不同的值初始化(!):

this is error

当我用 TextBoxFor 替换 DropDownListFor 时:

@Html.TextBoxFor(m => m.AgentTypes[i])

I在输入中获取正确的值(!):

this is it Should be

这意味着 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(!):

this is wrong

When I replace DropDownListFor with TextBoxFor:

@Html.TextBoxFor(m => m.AgentTypes[i])

I get the correct values in the inputs (!):

this is how it should be

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-11-02 11:58:07

我知道这篇文章已经有一年多了,但它似乎仍然是一个错误。替换

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

@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])
}

适合我的作品。

I know that this post is more than a year old but it seems still to be a bug. Replacing

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

with

@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])
}

works for me.

陈独秀 2024-11-02 11:58:07

注释掉你的第一行,
@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
然后会发生什么?

试试这个:

        @for (int i = 0; i < Model.AgentTypes.Length;  i++)
        {     
            string selected = Model.AgentTypes[i];
            @Html.DropDownListFor(m => selected, new SelectList(Model.AgentTypeListItems, "Text", "Value",Model.AgentTypes[i]))
        }

comment out your first line,
@Html.DropDownListFor(m => m.AgentType, Model.AgentTypeListItems)
what happens then?

Try this:

        @for (int i = 0; i < Model.AgentTypes.Length;  i++)
        {     
            string selected = Model.AgentTypes[i];
            @Html.DropDownListFor(m => selected, new SelectList(Model.AgentTypeListItems, "Text", "Value",Model.AgentTypes[i]))
        }
余罪 2024-11-02 11:58:07

我想知道这是否类似于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文