asp.net mvc default model binding problem

Posted by csetzkorn on Stack Overflow See other posts from Stack Overflow or by csetzkorn
Published on 2010-05-12T10:07:59Z Indexed on 2010/05/12 10:14 UTC
Read the original article Hit count: 249

Filed under:

I have some problems with ASP.NET MVC’s default model binder. The View contains HTML like this:

<input name="SubDTO[0].Id" value="1" type="checkbox">
<input name="SubDTO[1].Id" value="2" type="checkbox">

This is my simplified ‘model’:

public class SubDTO
{
    public virtual string Id { get; set; }
}

public class DTO
{
    public List<SubDTO> SubDTOs { get; set; }

    public DTO()
{
    SubDTOs = new List< SubDTO>();
}
}

All this works fine if the user selects at least the first checkbox (SubDTO[0].Id). The controller ‘receives’ a nicely initialised/bound DTO. However, if the first check box is not selected but only, for example, SubDTO[1].Id the object SubDTOs is null. Can someone please explain this ‘strange’ behaviour and how to overcome it? Thanks.

Best wishes,

Christian

© Stack Overflow or respective owner

Related posts about asp.net-mvc