Lets say a have the following Model
public class FirstModel
{
public List<SecondModel> SecondModels { get; set; }
}
public class SecondModel
{
public string Value { get; set; }
}
And this View
@model FirstModel
....
<input name="SecondModels[0].Value" value="test1"/>
<input name="SecondModels[1].Value" value="test2"/>
<input name="SecondModels[2].Value" value="test3"/>
...
The Modelbinding works good. I get the complete Model to my ActionMethod.
But if i remove the item in the middle using, for example, jQuery.
<input name="SecondModels[0].Value" value="test1"/>
<input name="SecondModels[2].Value" value="test3"/>
i lost the SecondModels[2] because the ModelBinder is unable to bind.
So i get only the first (“test1”).
Is there another Syntax or CustomModelBinder which is able to bind this correctly, or do i need to replace the input names after removing the item ?
Thank you very much in advance!
You may use Non-Sequential Model Binding feature of ASP.NET MVC here. In your case, you need something as follows:
For more information, please have a look at Non-Sequential Indices part of Phil Haack’s blog post:
Model Binding To A List