Foreach on IEnumerable property and CheckBoxFor in ASP.Net MVC

后端 未结 2 1846
面向向阳花
面向向阳花 2020-12-30 07:48

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

相关标签:
2条回答
  • 2020-12-30 08:28

    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) %>
    
    0 讨论(0)
  • 2020-12-30 08:28

    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?

    0 讨论(0)
提交回复
热议问题