@ Html.HiddenFor在ASP.NET MVC列表不起作用 [英] @Html.HiddenFor does not work on Lists in ASP.NET MVC
问题描述
我使用一个包含列表作为属性的模型。我填充这个名单与我从SQL Server抢项目。我想要列表隐藏在视图中,并传递到POST操作。后来我可能要更多的项目添加到这个列表与jQuery这使得不适合扩张以后的数组。通常你会使用
I'm using a model that contains a List as a property. I'm populating this list with items I grab from SQL Server. I want the List to be hidden in the view and passed to the POST action. Later on I may want to add more items to this List with jQuery which makes an array unsuitable for expansion later on. Normally you would use
@ Html.HiddenFor(型号=> model.MyList)
@Html.HiddenFor(model => model.MyList)
要实现该功能,但由于某种原因在POST列表始终为空。
to accomplish this functionality, but for some reason the List in POST is always null.
很简单的问题,任何人都知道为什么MVC的行为就像这样?
Very simple question, anyone know why MVC behaves like this?
推荐答案
我刚刚遇到这个问题,并解决它简单地通过执行以下操作:
I've just come across this issue and solved it simply by doing the following:
for(int i = 0; i < Model.ToGroups.Count; i++)
{
@Html.HiddenFor(model => Model.ToGroups[i])
}
通过使用了,而不是一个foreach模型结合将正常工作,拿起所有的隐藏值的列表。好像要解决这个问题的最简单的方法。
By using a for instead of a foreach the model binding will work correctly and pick up all of your hidden values in the list. Seems like the simplest way to solve this problem.
这篇关于@ Html.HiddenFor在ASP.NET MVC列表不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!