ASP.NET MVC 2 JQuery POST not displaying the model state errors

Posted by Oshan on Stack Overflow See other posts from Stack Overflow or by Oshan
Published on 2010-03-29T03:31:37Z Indexed on 2010/03/29 3:33 UTC
Read the original article Hit count: 788

Hello,

I have been using asp.net mvc for a bit (but I'm still a beginer). I want to have the ability to update two views as a result of a jquery postback.

Basically I have a list and a details view. The details view is presented using a jquery popup (using jquery-UI popup). I only want to update the list if the details save is successful (i.e. there are no validation errors on the details view). However, if there are any validation errros in the details view, I want to update the details view so that the user sees the validation errors.

so I thought in my controller, I return a JsonResult instead of a View.

[HttpPost]
public ActionResult SavePersonInfo(Person p) {
    if(ModelState.Valid) {
        return View("PersonList");
    }

    return Json({Error = true, View = PartialView("PersonDetails", p)});
}

As you can see if there are no errors I return the person list view, but if there are any validation errors, I have return the details view. The reason that I'm returning a JsonResult is I need to tell my view there is an error so that the view (jquery) knows which section to update (as in whether to update the person list 'div' or the popup dialog 'div').

So, in my view, the jquery is as follows (please assume that there is a form for entering in the person details and "SubmitPersonForm();" function is called upon clicking on the "Save" button):

<script type="text/javascript>
    $('#btnSave').click(function (event) {
            onBegin();

            $.ajax(
            {
                type: "POST",
                url: "/Person/Save",
                data: $('form').serialize(),
                success: function (result) {
                 if(result.Error) {
                        $('#dvDetails').html($(result).View));
                    }
                    else {
                        $('#dvPersonList').html($result);
                    }  
                }
            });
        });
</script>

So the problem that I have now, is that when there is a validation error, I do see the correct, 'div' being updated, but I lose the asp.net mvc validation messages. I do not see any validation errors in red, as if ASP.NET MVC is completely ignored them. However, my ModelState does have those errros, just not displayed in the details view. I do have valication summary and Html.ValidationFor(m => ...) statements put in my details view.

Could someone tell me why I'm not seeing the validation errors? although I'm using a JSonResult, I do use the right property which is a valid view when I render the 'dvDetails'. Am I doing something that I'm not suppose to in asp.net mvc? Btw I'm using asp.net mvc2 RC with Visual Studio 2010 RC.

Thank you.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2-validation