Diana Dimitrova
Diana Dimitrova

Reputation: 135

Updating the view with the new data in the viewmodel in MVC3, C#

I have ajax call of action in the controller, after it update the database and it is completed successfully I can do like this:

return PartialView("Overview", mydatamodel);

and then in the the success to do like this:

success: function (data) { 
            // do something with the data => refresh some
            // portion of your DOM
            $('#someDivId').html(data);
        }

And it would work fine, but what I need is that a collection in the view model to be updated, and the whole view to be rendered again with the new data.

I can do that if for instance I have submit button, then whole view is updated with the new data but if I have ajax call, how can i do that.

Here is link to my previous post where have more details: MVC3 receiving the new model data after submit

Thank you in advance!

Upvotes: 1

Views: 571

Answers (2)

twoflower
twoflower

Reputation: 6830

You usually use AJAX precisely for the situations where you only want a portion of the view to be updated after the request completion.

If you, for some reason, need to use AJAX even for such cases (e.g. using a DELETE HTTP verb to send the request), you might do something like

window.location.href = '/Items/123';

in your success callback function, which effectively triggers a full page update.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

If you want to update the entire view don't use AJAX. Simply use a submit button. The whole point of AJAX is to update only a portion of the view without navigating away from the current page.

By the way you could redirect on the client side using window.location.href:

success: function (data) { 
    window.location.href = '@Url.Action("Overview", "SomeController")';
}

but there's really no need to do that if you will always redirect in the success AJAX callback. You should not use AJAX in this scenario.

Upvotes: 3

Related Questions