1

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.

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

2 Answers2

0

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:

$.ajax({
  url: '@Url.Action("MyMethod", "MyController")',
  data: { id: 123 }, // i.e. someParameters 
  context: document.body,
  success: function(){
     window.location = '@Url.Action("Second", "MyController")';
  }
});

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.

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • But the redirect gets hit and the Second method gets hit. – petko_stankoski Jan 18 '12 at 13:31
  • i *think* it's still related to the fact that it's an ajax call. i'm not on a dev machine right now or would try, but give my *attempt* a run thro and see what happens. i'll do likewise at some point later today too.. – jim tollan Jan 18 '12 at 13:34
  • But I don't call Second from .cshtml. I call it from the controller. I can't call it from Ajax. – petko_stankoski Jan 18 '12 at 13:36
  • 1
    hmm - ok, i'll ponder and get back to you. perhaps a little more code in each action would help, just to clairfy the intention and technical limits that we are dealing with. I guess what i'm saying is - is it possible to refactor so that you have a vanilla action that returns your SecondView?? – jim tollan Jan 18 '12 at 13:39
  • I added some details in the question. – petko_stankoski Jan 18 '12 at 13:44
  • It can't be vanilla view, it has to have some ViewData objects. – petko_stankoski Jan 18 '12 at 13:46
  • Srcee, if you need viewdata objects, then i don't think a javascript/ajax call is appropriate in this case. remember that you are not writing to a response stream, you are merely issuing a method call and waiting for a return (in the client) of that call. typically, that would be either json and/or a partialview that would be returned and populated into 'some div'. – jim tollan Jan 18 '12 at 14:06
0

Try not calling Response.Redirect(...) in MyMethod. Instead, call:

public ActionResult MyMethod()
{
  return Redirect(Url.Action("Second"));
}

Note that if the controller name is the same as the one you are in, you don't need to explicitly include it.

I just did a test on my dev box, and this works (assuming I correctly modeled your problem).

JasCav
  • 34,458
  • 20
  • 113
  • 170
  • @Srcee - Can you post more of your code on the related pages? I feel like we're not seeing enough. Based on your discussion with jim above, I feel like there is something I'm missing. – JasCav Jan 18 '12 at 15:47