I have a model like

public class A {
   // Properties - some of them are required some optional
   // Optional
   public class ICollection<B> Items { get; set; }
}
public class B {
    public string A {get;set;} // Required
}

To use the Html.EditoFor(o => o.Items[0]) I must add an empty object to Items and as such this object is sent to the server causing ModelState.IsValid to be false (if the user didn't choose to add any Bs.

How is the proper way of handling this? What would be an option is to create an [Optional] attribute for collections that could inspect the list and check if it contains just one object and if that object is the default object, then skip it.

The Items property is filled on client side by calling the controller that renders the partial view (Html.EditoFor(o => o.Items[0])) and then some more js-fiddle to ensure all indexs are correct.

Thanks!

Related posts

Recent Viewed