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

前端 未结 9 755
春和景丽
春和景丽 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:52

    I had a reference entity that wasn't supposed to be validated.

    Removed it from validation at the beginning of the action:

    [HttpPost]
    public async Task Post([FromBody] Contact contact)
    {
      var skipped = ModelState.Keys.Where(key => key.StartsWith(nameof(Contact.Portfolios)));
      foreach (var key in skipped)
        ModelState.Remove(key);
        //ModelState doesn't include anything about Portfolios which we're not concerned with
    
      if (!ModelState.IsValid)
        return BadRequest(ModelState);
    
      //Rest of action
    }
    

提交回复
热议问题