ASP.NET MVC POST 中的模型绑定 IEnumerable?

发布于 2024-12-26 03:20:32 字数 711 浏览 2 评论 0原文

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

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

发布评论

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

评论(2

゛清羽墨安 2025-01-02 03:20:32

模型绑定取决于生成的 HTML 的外观。为了正确绑定您的特定场景,HTML 应该如下所示:

<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 方法会阻止系统为嵌套输入生成正确的名称。您可以采取以下措施来解决此问题
首先,在视图模型中更改

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

为,

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

以便您不必在循环中调用 ToArray 方法,并为输入生成正确的名称。
其次,制作一个编辑器模板并将其放入当前控制器下的编辑器模板文件夹中或共享文件夹的编辑器模板文件夹中。使此视图接受 BandAvailabilityInfo 类型的模型,并且此视图的名称也应为 BandAvailabilityInfo。然后在你的主视图中你只需要替换整个循环

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

,其余的将由框架本身处理

Model binding depends upon what the generated HTML looks like. For your particular scenario to bind properly, the 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"/>

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; }

to

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

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

一直在等你来 2025-01-02 03:20:32

它适用于 IEnumerable 类型。
我认为问题在于您在 for 循环内编写的 ToArray 。 (效率也不是很高)
for 循环更改为 foreach

请参阅这个优秀答案如何实现它。

It works fine with the IEnumerable type.
I think the problem is the ToArray you are writing inside the for loop. (It's not very efficient either)
change the for loop to foreach.

See this excellent answer how to achieve it.

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