Is there any way to ignore some properties (on a POCO) when validating a form in ASP.NET MVC3?

前端 未结 9 592
春和景丽
春和景丽 2021-02-05 05:59

i\'ve got a sign up wizard for new user registration. When I try to goto the 2nd page, I get validation errors because my User object hasn\'t been fully populated,

9条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 06:43

    In the action just remove the errors for the items not yet checked for. This then makes your model valid for the items already checked

    foreach (var error in ModelState["Avatar"].Errors)
     {
          ModelState["Avatar"].Errors.Remove(error);
     }
    

    or

    ModelState["Avatar"].Errors.Clear();
    

提交回复
热议问题