Foreach on IEnumerable property and CheckBoxFor in ASP.Net MVC

20,291

Solution 1

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?

Solution 2

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) %>
Share:
20,291
Mike Therien
Author by

Mike Therien

Software developer mostly working in Microsoft Web Technologies

Updated on December 21, 2020

Comments

  • Mike Therien
    Mike Therien over 3 years

    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 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:

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

    In ListRights.ascx, I want code like this:

    <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.

    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:

    <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?

  • Craig
    Craig over 13 years
    Thanks! This has made my life easier!
  • Scott Baker
    Scott Baker almost 13 years
    You save me mucho pain, amigo. Gracias.
  • mare
    mare almost 12 years
    I don't really get this answer, why would you go through all this trouble?
  • mare
    mare almost 12 years
    ok, after some more thinking and going to Steven's blog I see what you are trying to achieve. Steve's solutions does work in this case. Though I don't think that user experience is any good in this case - editing multiple items this way and posting all of them back to controller in one go.