ASP.NET MVC 模型绑定器无法使用字典

发布于 2024-09-08 18:11:01 字数 1981 浏览 11 评论 0原文

给定使用 DefaultModelBinder 的以下视图模型和操作,它似乎忽略了字典,但正确绑定了所有其他属性。我在这里错过了什么吗?查看 MVC 源代码,这似乎是合法的。

谢谢

public class SomeViewModel
{
    public SomeViewModel()
    {
        SomeDictionary = new Dictionary<string, object>();
    }

    public string SomeString { get; set; }
    public IDictionary<string, object> SomeDictionary { get; set; }
}

[HttpPost]
public ActionResult MyAction(SomeViewModel someViewModel)
{
    //someViewModel.SomeString binds correctly
    //someViewModel.SomeDictionary is null
}

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeViewModel>" MasterPageFile="~/Views/Shared/Site.Master" %>
<asp:Content runat="server" ID="Content2" ContentPlaceHolderID="MainContent">
<% using (Html.BeginForm("MyAction", "MyController")) {%>

    <%= Html.EditorFor(m => m.SomeString) %>
    <%= Html.EditorFor(m => m.SomeDictionary["somevalue"]) %>

    <input type="submit" value="Go" />
<%} %>
</asp:Content>

,作为参考,HTML 输出是:

<input class="text-box single-line" id="SomeString" name="SomeString" type="text" value="" />
<input class="text-box single-line" id="Somedictionary_somevalue_" name="SomeDictionary[somevalue]" type="text" value="" />

编辑:上面的内容不会像下面指出的那样工作,但是我更喜欢这种布局,并且以下快速破解可以满足我的需要,在发布后立即调用它...

someViewModel.SomeDictionary = (from object key in Request.Form.Keys
                                where key.ToString().StartsWith("SomeDictionary[")
                                select new
                                {
                                    Key = key.ToString().Replace("SomeDictionary[", string.Empty).Replace("]", string.Empty),
                                    Value = (object)Request.Form[key.ToString()]
                                }).ToDictionary(arg => arg.Key, arg1 => arg1.Value);

它需要一些整理当然 :)

Given the following view model and action using the DefaultModelBinder, it seems to ignore the dictionary, but bind all other properties correctly. Am I missing something here? Looking at the MVC source code this seems legit.

Thanks

public class SomeViewModel
{
    public SomeViewModel()
    {
        SomeDictionary = new Dictionary<string, object>();
    }

    public string SomeString { get; set; }
    public IDictionary<string, object> SomeDictionary { get; set; }
}

[HttpPost]
public ActionResult MyAction(SomeViewModel someViewModel)
{
    //someViewModel.SomeString binds correctly
    //someViewModel.SomeDictionary is null
}

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeViewModel>" MasterPageFile="~/Views/Shared/Site.Master" %>
<asp:Content runat="server" ID="Content2" ContentPlaceHolderID="MainContent">
<% using (Html.BeginForm("MyAction", "MyController")) {%>

    <%= Html.EditorFor(m => m.SomeString) %>
    <%= Html.EditorFor(m => m.SomeDictionary["somevalue"]) %>

    <input type="submit" value="Go" />
<%} %>
</asp:Content>

And for reference, the HTML output is:

<input class="text-box single-line" id="SomeString" name="SomeString" type="text" value="" />
<input class="text-box single-line" id="Somedictionary_somevalue_" name="SomeDictionary[somevalue]" type="text" value="" />

EDIT: The above will not work as pointed out below, however I prefer this layout and the following quick hack works for my needs, call this just after posting...

someViewModel.SomeDictionary = (from object key in Request.Form.Keys
                                where key.ToString().StartsWith("SomeDictionary[")
                                select new
                                {
                                    Key = key.ToString().Replace("SomeDictionary[", string.Empty).Replace("]", string.Empty),
                                    Value = (object)Request.Form[key.ToString()]
                                }).ToDictionary(arg => arg.Key, arg1 => arg1.Value);

It needs some tidying up ofcourse :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

自找没趣 2024-09-15 18:11:01

您可以查看这篇文章来了解应该如何绑定字典。恐怕使用强类型 EditorFor 帮助程序将无法实现此目的,并且您必须手动生成字段。

You may take a look at this post to see how dictionaries should be binded. I am afraid that using strongly typed EditorFor helpers you won't be able to achieve this and you will have to generate the fields manually.

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