Eyal
Eyal

Reputation: 4763

Validation state of model property

I would like to know if I can get the property state of 'model.Email' in the following code. I can validate the entire model state but don't know how to validate each property state.

    [HttpPost]
    public JsonResult RegisterFromLogin(LoginModel model, string returnUrl)
    {            
        if (!ModelState.IsValid)
            return Json(new { success = false, message = "Validation failed!" });

        if (!model.Email.Isvalid)                                          
         {
                //  I am trying to do something like this
                //This if statement code is not valid!
         }

    }

Thanks.

Upvotes: 3

Views: 770

Answers (1)

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

ModelState has an indexer that accepts the name of the property you're interested in. In this instance I assume your property is called Errors. Try the following:

if (ModelState["Email"].Errors.Any()) ...

Upvotes: 1

Related Questions