0

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:

  1. in first time action all thing is Ok(validation done)
  2. modelState.Clear
  3. remove items in array that status is delete
  4. 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; }          
}
dandan78
  • 13,328
  • 13
  • 64
  • 78
Mohammad Akbari
  • 4,486
  • 6
  • 43
  • 74
  • 1
    What is your expectation besides "it doesn't work". Are you expecting the validation to fail after you filter out some of the array items? – Rick B Dec 10 '12 at 15:01
  • i expect after remove deleted item from array, revalidate current items in array, but after Clear ModelState , and tryValidateModel, ModelState is true, While model not Valid – Mohammad Akbari Dec 11 '12 at 06:10
  • I expect after remove deleted item from array, revalidate current items in array, but after Clear ModelState , and tryValidateModel, validation work for simple property, but not work for Complex property – Mohammad Akbari Dec 11 '12 at 06:17

0 Answers0