My model has some property, one of them is Array of Class, and for some Property in Model and array class use from datannotation attribute. In view user fill some row as array and other model property, and may delete some of row(arrayProperty), me set for deleted row Status:delete, and in action remove deleted row from array, steps in action is:
- in first time action all thing is Ok(validation done)
- modelState.Clear
- remove items in array that status is delete
- use TryValidateModel for validate refined model
this work for all proprty in Model except array property
Action code:
public ActionResult Test(ViewModel model)
{
ModelState.Clear();
model.Array = model.Array.Where(p => p.Status != false).ToArray();
if(ModelState.IsValid){}
TryValidateModel(model);
return View();
}
Class code:
public class ViewModel
{
public TestClass[] Array { get; set; }
[Required]
public string Family { get; set; }
}
public class TestClass
{
[Required]
public string Name { get; set; }
public int? Age { get; set; }
public bool Status { get; set; }
}