开发者

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

开发者 https://www.devze.com 2022-12-25 12:02 出处:网络
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.

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 y开发者_如何学JAVAou 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.


The Url does not match the Action name ... not sure if you maybe are calling the wrong Method on the controller ... just an idea. :-)


ASP.NET MVC code processes the response to display validation errors stored in the ModelState during a postback. Because you are using an Ajax Post, the postback code is not rebuilding the page and displaying the validation errors. If you want to display validation errors, you have to handle it yourself.

I've done it by passing the ModelState errors as an array in the response. I process my Ajax response to update the validation error placeholder elements in the form with corresponding error messages in the response.

A warning, though: the keys in the ModelState may have their case changed, so an element ID string in the form doesn't exactly match the string that's used for the key. ModelStateDictionary is case-insensitive, but DOM ID's are not.

0

精彩评论

暂无评论...
验证码 换一张
取 消