In the file FirstView.cshtml I have an event – clicking to a cell of a table redirects you to a method:
$.get('@Url.Action("MyMethod", "MyController")', { someParameters });
In the method MyMethod, which is void, I call and event DownloadStringCompleted in which I have:
Response.Redirect(Url.Action("Second", "MyController"));
Second is an action method which returns SecondView. SecondView is my desired view but it never shows on the browser. The breakpoint enters in it, passes it and I get FirstView in the browser.
What could be the reason? Can it be because they are in the same controller?
The methods are like this:
Second return View. In its body, I pass some ViewData parameters.
The event:
I read some JSON data and call the redirection.
The strange thing is that the breakpoint moves through the correct view, but I get the wrong one in my browser.
Srcee,
I believe this is due to the fact that your ajax function is ‘done’ once it has hit the MyMethod action. The redirect doesn’t get fired as it’s not part of the pipeline due to the ajax call (at least, this is my basic understanding). An alternative for you would be to do something along the following lines:
this is all top of the head stuff and may not work, but is an alternative approach.
[edit] – based on your later edit, i would actually suggest that you don’t use ajax at all as it’s impossible (as per my comments below) to redirect on the server via an ajax call. see:
asp.net mvc ajax post – redirecttoaction not working
and also:
How can I RedirectToAction within $.ajax callback?
for confirmation on this.