在ASP.NET MVC POST Modelbinding IEnumerable的? [英] Modelbinding IEnumerable in ASP.NET MVC POST?

查看:100
本文介绍了在ASP.NET MVC POST Modelbinding IEnumerable的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否与modelbinding的IEnumerable类型的MVC POST?

Is there any issues with modelbinding IEnumerable types to an MVC POST?

在我的模型的一些性质不被在后一个动作的约束。似乎像弦模型属性都OK,但我的IEnumerable的是什么是不是被约束。

Some properties in my Model are not being bound upon a post to an action. Seems that properties on the model like strings are ok, but my IEnumerable is what is not being bound.

下面是我的code的一个片段:

Here's a snippet of my code:

<%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.Test) %>
    <% for (int i = 0; i < Model.ResponseInfo.SubsetInfo.BandAvailabilities.Count(); i++)
    {%>
        <%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities.ToArray()[i].BandName) %>
  <% } %>

和这里是这些属性看起来像模型:

And here is what those properties look like in the model:

public IEnumerable<BandAvailabilityInfo> BandAvailabilities { get; set; }
public string Test { get; set; }

视图工作正常,并输出具有在其中的预期值的文本框的列表。但被炒鱿鱼后行动只能识别测试字符串作为属性。该模型的状态不包含我的IEnumerable的数据无论是。

The view works fine and outputs a list of textboxes with the expected values in them. But the post Action which gets fired only recognises the Test string as a property. The model state does not contain my IEnumerable data either.

推荐答案

示范约束力取决于生成html的样子。乌拉圭回合特定场景绑定正确的HTML应该像

Model binding depends upon how generated html looks like. for ur particular scenario to bind properly html should look like

<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[0].BandName"/>
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[1].BandName"/>
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[2].BandName"/>
.
.
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[n].BandName"/>

我还没有尝试过,但我几乎可以肯定,在循环调用ToArray的方法是保持从生成嵌套输入适当的名称系统。有两件事情可以做,以解决这个问题
首先,在您的视图模型的变化

i have not tried it but i am almost certain that call to ToArray method in loop is keeping the system from generating proper names for nested inputs. There are couple of things you can do to remedy this First, in your view model change

public IEnumerable<BandAvailabilityInfo> BandAvailabilities { get; set; }

public IList<BandAvailabilityInfo> BandAvailabilities { get; set; }  //or Array

所以你不必调用ToArray的方法循环,​​正确的名字输入产生做。
二,制作编辑模板,并把它放到编辑模板文件夹下的任何电流控制器或者共享文件夹的编辑模板文件夹。使这种观点接受这个观点的类型 BandAvailabilityInfo 和名称的模式也应 BandAvailabilityInfo 。然后在主视图只有更换整个循环

so you don't have to call ToArray method in the loop and proper names are generated for inputs. Second, make an editor template and put it in Editor templates folder either under the current controller or in shared folder's Editor template folder. Make this view accept model of type BandAvailabilityInfo and name of this view should also be BandAvailabilityInfo. then in your main view you only have to replace entire loop with

 <%: Html.EditorFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities%>

和余下的由框架本身来处理

and rest will be handled by framework itself

这篇关于在ASP.NET MVC POST Modelbinding IEnumerable的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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