Skip to content Skip to sidebar Skip to footer

Foreach On IEnumerable Property And CheckBoxFor In ASP.Net MVC

I believe this question applies to any of the 'For' Html helpers, but my specific problem is using CheckBoxFor... I have a model that is of type IEnumerable, where rights is a simp

Solution 1:

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

Solution 2:

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?


Post a Comment for "Foreach On IEnumerable Property And CheckBoxFor In ASP.Net MVC"