绑定包含MVC4列表的字典 [英] Binding a dictionary containing a list in MVC4

查看:200
本文介绍了绑定包含MVC4列表的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法张贴,里面有包含一个int作为重点,并作为一个值对象列表的字典的形式。

I am having trouble posting a form that contains a Dictionary that contains an int as a key and a list of objects as a value.

本来这只是对象的列表和工作的罚款和类型是:

Originally this was just a List of Objects and that worked fine and the type was:

List<MyObject> Fields

工作的标记是

Fields_{0}__Property1

其中{0}是对象的索引。为了得到它回发对象的列表我呈现的对象,像这样隐藏字段:

where {0} is the index of the object. To get it to post back the List of Objects I rendered the object with hidden fields like this:

  @Html.HiddenFor(m => m.Property1, new { Name = string.Format("Fields[{0}].Property1", Model.Index), @id = string.Format("Fields_{0}__Property1", Model.Index) })

这行之有效。但是现在,我们有一本字典而不是列表,并将列表是字典里。

This worked well. Now however, we have a dictionary instead of a list and the list is inside the dictionary.

现在的类型是:

Dictionary<int, List<MyObject>>.

我测试的时候我们使用Html.HiddenFor渲染字典,所以我已经添加所需格式现在是隐藏字段的格式为:

I tested the format expected when we render the dictionary using Html.HiddenFor and so I've added hidden fields with the required format which now is:

  @Html.HiddenFor(m => m.Property1, new { Name = string.Format("Fields[{0}][{1}].Property1", Model.Index, Model.Position), @id = string.Format("Fields_{0}__{1}__Property1", Model.Index, Model.Position) })

现在id字段是

Fields_{0}__{1}__Property1

其中{0}是字典和{1}是该列表中的对象的索引

where {0} is the key of the dictionary and {1} is the index of the object in the list.

不过在回发我现在得到

 [InvalidCastException: Specified cast is not valid.]
   System.Web.Mvc.CollectionHelpers.ReplaceDictionaryImpl(IDictionary`2 dictionary, IEnumerable`1 newContents) +131

我猜MVC是足够聪明的渲染上的看法,但不够聪明这个复杂对象的字段来收集它们放回视图模型,当我们回来后。

I am guessing MVC is smart enough to render the fields of this complex object on the view but not smart enough to collect them back into the viewmodel when we post back.

我发现<一个href=\"http://stackoverflow.com/questions/16929162/bind-to-a-dictionary-of-lists-of-lists-of-bools-with-a-strongly-typed-mvc-view-b\">this其他人谁这里有一个类似的问题,他不使用一本字典,而是创建一个复杂的对象,解决了这个问题。然而,我想知道,如果有,将不需要我重写整个系统更快的方法。

I found this other guy who had a similar problem here and he solved it by not using a dictionary but instead creating a complex object. I'm wondering, however, if there's a quicker way that won't require me to rewrite the entire system.

任何想法?

更新

我采取DefaultModelBinder源$ C ​​$ c和调整它解决它。
我找到源<一个href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Mvc/DefaultModelBinder.cs\"相对=nofollow>此处。因为我希望所有的高级功能和验证规则适用于所有其他元素我没有创建自己的活页夹。
一旦我得到了DefaultModelBinder编译和工作,我发现那里的字典是失败投复杂的项目的一部分,并写道,解决了这个问题的自定义词典更新方法

I solved it by taking the source code of DefaultModelBinder and adjusting it. I found the source here. I didn't create my own Binder because I want all the advanced functionality and validation rules to apply to all other elements. Once I got the DefaultModelBinder compiling and working I found the part where the dictionary was failing to cast the complex items and wrote a custom Dictionary update method that solved the problem

推荐答案

您可以随时创建一个自定义模型绑定绑定完全按照你想从请求值对象。简单地创建一个实现 System.Web.Mvc.IModelBinder 接口的类并实施 BindModel()方法。

You can always create a custom Model Binder to bind objects from request values exactly as you want. Simply create a class that implements the System.Web.Mvc.IModelBinder interface and implement the BindModel() method.

这篇关于绑定包含MVC4列表的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆