MVC ModelState Validation of List of Complex Properties
<p>So, I have a <code>User</code> Model class. It has List of <code>UnitViewModel</code>.</p>

<pre><code>public class UserViewModel
{
    [Required(ErrorMessage="FirstName")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "LastName")]
    public string LastName { get; set; }

    public List&lt;UnitViewModel&gt; DesiredUnits { get; set; }
}

public class UnitViewModel
{
    public Guid? UnitID { get; set; }
    [Required(ErrorMessage = "NAMEEE")]
    public string Name { get; set; }

    public bool Selected { get; set; }
}
</code></pre>

<p>When I try to create new <code>User</code> I post <code>UserViewModel</code> from my view. Here I get <code>ModelState.IsValid</code> <code>false</code>. This happens because Validation also checks the <code>UnitViewModel.Name</code> property which is null (and it is normal since I create <code>User</code> and I have <code>checkbox</code> list for <code>UnitViewModel</code>. User selects <code>UnitViewModel</code> from existing <code>UnitViewModel</code> list).
<br><br>
I could fix it in a horrible way of <code>ModelState["DesiredUnits[0].UnitID"].Errors.Clear()</code> by foreach loop but I won't.
<br><br>
So, what would be the solution? How can I disable validation of list of complex property of a model?</p>

<p><strong>UPDATE</strong> <br>
Another solution, I am currently using, is to have <code>HiddenFor(m=&gt;m.Name)</code> in <code>UnitViewModel</code>'s editor template but disadvantage with this approach is that <code>Name</code> is unnecessary when I already have label in editortemplate. Also, if I had any more required fields I would have to have corresponding hidden input for each of them.</p>