asp.net-mvc ASP.Net MVC 中 IEnumerable 属性和 CheckBoxFor 上的 Foreach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2409552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Foreach on IEnumerable property and CheckBoxFor in ASP.Net MVC
提问by Mike Therien
I believe this question applies to any of the "For" Html helpers, but my specific problem is using CheckBoxFor...
我相信这个问题适用于任何“For”Html 助手,但我的具体问题是使用 CheckBoxFor...
I have a model that is of type IEnumerable, where rights is a simple POCO. This model is actually a property of a bigger model that I created an EditorTemplate for. Here is the bigger picture of my model:
我有一个 IEnumerable 类型的模型,其中权限是一个简单的 POCO。这个模型实际上是我为其创建了一个 EditorTemplate 的更大模型的一个属性。这是我的模型的大图:
public class bigmodel
{
public string Title {get; set;}
public string Description {get; set;}
[UIHint("ListRights")]
public IEnumerable<rights> Rights {get;set;}
}
public class rights
{
public bool HasAccess {get; set;}
public string Description {get;set;}
}
I created an editortemplate called "ListRights" that my main view uses. For example: <%=Html.EditorFor(m => m.Rights) %>.
我创建了一个名为“ListRights”的编辑器模板,我的主视图使用了它。例如:<%=Html.EditorFor(m => m.Rights) %>。
In ListRights.ascx, I want code like this:
在 ListRights.ascx 中,我想要这样的代码:
<table>
<% foreach(rights access in Model)
{ %>
<tr>
<td>
<%=Html.CheckBoxFor( access ) %>
</td>
<td>
<%=access.Description %>
</td>
</tr>
<% } %>
</table>
I know the CheckBoxFor line does not work, but I want to do something that generates the same result, as if access was a property on the model.
我知道 CheckBoxFor 行不起作用,但我想做一些产生相同结果的事情,就好像访问是模型上的一个属性一样。
In the above example, I would like everything to autobind on post.
在上面的例子中,我希望一切都在发布时自动绑定。
I've tried faking the CheckBox with code similar to this, but it doesn't autobind:
我试过用与此类似的代码伪造 CheckBox,但它不会自动绑定:
<table>
<% for(int i=0; i < Model.Count(); i++)
{ %>
<tr>
<td>
<%=Html.CheckBox(string.Format("[{0}].HasAccess",i), Model.ElementAt(i).HasAccess)%>
</td>
<td>
<%=access.Description %>
</td>
</tr>
<% } %>
</table>
Any suggestions?
有什么建议?
采纳答案by Mike Therien
I found the answer by using a blog post by Steve Sanderson at http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
我通过使用 Steve Sanderson 在http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/的博客文章找到了答案
Using "Html.BeginCollectionItem" worked in my situation.
使用“Html.BeginCollectionItem”在我的情况下有效。
I created an EditorTemplate for rights (in my example). Then added Steve's BeginCollectionItem to that template. I called the template using Html.RenderPartial as suggested in Steve's blog.
我为权限创建了一个 EditorTemplate(在我的示例中)。然后将 Steve 的 BeginCollectionItem 添加到该模板中。我按照 Steve 的博客中的建议使用 Html.RenderPartial 调用了模板。
I wanted to use Html.EditorFor(m => m.item), but that doesn't work because item is in the ForEach and not in the model. Could EditorFor be used in this case?
我想使用 Html.EditorFor(m => m.item),但这不起作用,因为 item 位于 ForEach 而不是模型中。在这种情况下可以使用 EditorFor 吗?
回答by mare
I guess you had problems because this didn't work
我猜你有问题,因为这不起作用
<%=Html.CheckBoxFor(access) %>
and this didn't work either
这也不起作用
<%=Html.CheckBoxFor(access=>access.HasAccess) %>
but this should work
但这应该有效
<%=Html.CheckBoxFor(x=>access.HasAccess) %>

