如何ASP.NET MVC:我怎样才能绑定类型列表℃的财产; T&GT ;? [英] How ASP.NET MVC: How can I bind a property of type List<T>?
问题描述
比方说我有波纹管模式
public class UserInformation
{
public List<UserInRole> RolesForUser { get; set; }
//Other properties omitted...
}
public class UserInRole
{
public string RoleName { get; set; }
public bool InRole { get; set; }
}
在我的网页我有类似
<%using(Html.BeginForm()){%>
.../...
<%for(int i =0; i<Model.InRoles.Cout; i++%>
<p><%: Html.CheckBox(Model.Roles[i].RoleName, Model.Roles[i].InRole)%></p>
<%}%>
我们的想法是能够检查/取消选中该复选框,这样,当窗体发布到动作时,动作通过添加/删除从每个角色的用户行为适当。
The idea is to be able to check/uncheck the checkbox so that when the form is posted to the action, the action acts appropriately by adding/removing the user from each role.
当形式被发送到该操作方法的问题是,在角色属性(它是一个列表UserInRole对象)不反映由用户进行的改变。 ModelBinder的正常运行上的所有其他属性,但角色属性
The problem is when form is posted to the action method, the Roles property (which is a list UserInRole object) doesn't reflect the change made by the user. ModelBinder works properly on the all other properties but 'Roles property'
我不知道如何能做到这一点。我怀疑给复选框的名称/ id是不恰当的。但是,我只是叠加。也许我应该做不同的看法。
I wonder how I can do that. I suspect that the name/id given to the checkbox is not appropriate. But, I'm just stack. Maybe I should do it differently.
感谢您的帮助。
推荐答案
您应该看到菲尔哈克对的模型绑定到一个列表。
从本质上讲,你需要的只是提交了一堆表单域了每个具有相同的名称。
You should see Phil Haack's post on model binding to a list. Essentially what you need to is simply submit a bunch of form fields each having the same name.
<%@ Page Inherits="ViewPage<UserInformation>" %>
<% for (int i = 0; i < 3; i++) { %>
<%: Html.EditorFor(m => m.RolesForUser[i].RoleName) %>
<%: Html.EditorFor(m => m.RolesForUser[i].InRole) %>
<% } %>
这篇关于如何ASP.NET MVC:我怎样才能绑定类型列表℃的财产; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!