views:

362

answers:

1

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.

A: 

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

Syska
Thank you for your reply. Sorry that is a typo in my question. I have made absolutely sure that the url is right. In fact I've debugged and it DOES call my controller. In fact, when validation fails, the ModelState contains the validation errors (as a result of my data annotations). Really not sure why I'm not seeing the errors in the View :(.
Oshan
Okay, just wanted to make sure ...Maybe its a limitaion when its redered as a PartialViewHave you looked at this: http://stackoverflow.com/questions/1556533/asp-net-mvc-modelstate-and-partial-view seems to be the same probmen.
Syska

related questions