Get error message if ModelState.IsValid fails?

前端 未结 11 1267
离开以前
离开以前 2020-11-30 02:27

I have this function in my controller.

[HttpPost]
public ActionResult Edit(EmployeesViewModel viewModel)
{
    Employee employee = GetEmployee(viewModel.Empl         


        
相关标签:
11条回答
  • 2020-11-30 02:33

    Ok Check and Add to Watch:

    1. Do a breakpoint at your ModelState line in your Code
    2. Add your model state to your Watch
    3. Expand ModelState "Values"
    4. Expand Values "Results View"

    Now you can see a list of all SubKey with its validation state at end of value.

    So search for the Invalid value.

    0 讨论(0)
  • 2020-11-30 02:34

    If anyone is here for WebApi (not MVC) you just return the ModelState object:

    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);

    0 讨论(0)
  • 2020-11-30 02:38

    Try this

    if (ModelState.IsValid)
    {
        //go on as normal
    }
    else
    {
        var errors = ModelState.Select(x => x.Value.Errors)
                               .Where(y=>y.Count>0)
                               .ToList();
    }
    

    errors will be a list of all the errors.

    If you want to display the errors to the user, all you have to do is return the model to the view and if you haven't removed the Razor @Html.ValidationFor() expressions, it will show up.

    if (ModelState.IsValid)
    {
        //go on as normal
    }
    else
    {
        return View(model);
    }
    

    The view will show any validation errors next to each field and/or in the ValidationSummary if it's present.

    0 讨论(0)
  • 2020-11-30 02:41
    publicIHttpActionResultPost(Productproduct) {  
        if (ModelState.IsValid) {  
            //Dosomethingwiththeproduct(notshown).  
            returnOk();  
        } else {  
            returnBadRequest();  
        }  
    }
    

    OR

    public HttpResponseMessage Post(Product product)
            {
                if (ModelState.IsValid)
                {
                    // Do something with the product (not shown).
    
                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                }
            }
    
    0 讨论(0)
  • 2020-11-30 02:43

    You can do this in your view without doing anything special in your action by using Html.ValidationSummary() to show all error messages, or Html.ValidationMessageFor() to show a message for a specific property of the model.

    If you still need to see the errors from within your action or controller, see the ModelState.Errors property

    0 讨论(0)
  • 2020-11-30 02:45

    I have no idea if this is your problem, but if you add a user and then change the name of your application, that user will remain in the database (of course), but will be invalid (which is correct behavior). However, there will be no error added for this type of failure. The error list is empty, but ModelState.IsValid will return false for the login.

    0 讨论(0)
提交回复
热议问题