Foreach on IEnumerable property and CheckBoxFor in ASP.Net MVC

感情迁移 提交于 2019-11-30 06:56:42

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) %>

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/

Using "Html.BeginCollectionItem" worked in my situation.

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.

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!