ASP.NET MVC POST 中的模型绑定 IEnumerable?
将 IEnumerable 类型模型绑定到 MVC POST 是否存在任何问题?
我的模型中的某些属性未绑定到操作的帖子上。似乎模型上的属性(如字符串)没问题,但我的 IEnumerable 没有被绑定。
这是我的代码片段:
<%: 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) %>
<% } %>
以下是这些属性在模型中的样子:
public IEnumerable<BandAvailabilityInfo> BandAvailabilities { get; set; }
public string Test { get; set; }
视图工作正常并输出一个文本框列表,其中包含预期值。但是被触发的后操作仅将测试字符串识别为属性。模型状态也不包含我的 IEnumerable 数据。
Is there any issues with modelbinding IEnumerable types to an MVC POST?
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.
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; }
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模型绑定取决于生成的 HTML 的外观。为了正确绑定您的特定场景,HTML 应该如下所示:
我还没有尝试过,但我几乎可以肯定,循环中调用
ToArray
方法会阻止系统为嵌套输入生成正确的名称。您可以采取以下措施来解决此问题首先,在视图模型中更改
为,
以便您不必在循环中调用
ToArray
方法,并为输入生成正确的名称。其次,制作一个编辑器模板并将其放入当前控制器下的编辑器模板文件夹中或共享文件夹的编辑器模板文件夹中。使此视图接受
BandAvailabilityInfo
类型的模型,并且此视图的名称也应为BandAvailabilityInfo
。然后在你的主视图中你只需要替换整个循环,其余的将由框架本身处理
Model binding depends upon what the generated HTML looks like. For your particular scenario to bind properly, the HTML should look like:
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 thisFirst, in your view model change
to
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 beBandAvailabilityInfo
. then in your main view you only have to replace entire loop withand rest will be handled by framework itself
它适用于
IEnumerable
类型。我认为问题在于您在 for 循环内编写的 ToArray 。 (效率也不是很高)
将
for
循环更改为foreach
。请参阅这个优秀答案如何实现它。
It works fine with the
IEnumerable
type.I think the problem is the
ToArray
you are writing inside thefor
loop. (It's not very efficient either)change the
for
loop toforeach
.See this excellent answer how to achieve it.