Matias Varini
Matias Varini

Reputation: 51

RedirectToAction MVC 3 after $.ajax call

From my view I am sending via $.ajax a JSON object to my controller to save it in the database. If all succeeded i want to redirect to another action which will show a diferent view.

If i use this code:

return RedirectToAction("CreatePage", "Survey", new {id = question.PageId});

The execution goes to the Survey controller which returns a view but it is not shown.

I have read some post which said that it is not posible to redirect via ajax.

The solution I use so far is to redirect via javascript like this:

success: function (ret) {
            window.location.href = "/Survey/CreatePage/" + $("#PageId").val();
         }

Although this always works, sometimes i need to refresh the CreatePage view to show the last changes made.

Any idea of how to solve this problem better?

Thanks in advance

Upvotes: 2

Views: 2854

Answers (3)

Matias Varini
Matias Varini

Reputation: 51

As mccow002 suggested, I wasn't really needing to make the call via AJAX for that part. After studying the solutions suggested, i realized that i could simple submit it in a form. My confusion came because I have a save and continue editing and a save. For the save and continue I use the AJAX call, but for the save option with the form being submitted is ok.

Thanks very much for your help.

Upvotes: 1

Ryand.Johnson
Ryand.Johnson

Reputation: 1906

You could use [OutputCache] on the CreatePage action so that it doesn't cache the page or only caches for so long.

output caching

Upvotes: 0

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

Instead of redirecting to a new page, you can send a rendered html from .net code back to client and load that html in page, like this $("#main").load(renderedHtml).

But for refreshing the page you can write a simple script that run at specified intervals and refresh the page contens.

Upvotes: 0

Related Questions