Da Wei
Da Wei

Reputation: 49

RedirectToAction fails to show the view

I have 2 controllers A and B, both of which have their "index.cshtml". I make an ajax call into a method of A which in turn calls RedirectToAction to a method of B. But the "index" view of B doesn't show up even though the ajax call succeeds. Ajax

@.ajax(function()
 {
   ...
   url:"A/MethodToRedirect",
   ...
 });
Controller A
{
   public IActionResult MethodToRedirect(param)
   {
       // do something
       return RedirectToAction("Index", "B");
   }
}

Upvotes: 0

Views: 107

Answers (1)

Serge
Serge

Reputation: 43959

You can't redirect this way using ajax. There are two ways :

  1. You use a partial view
return PartialView("Index");

and add to your ajax:

  success: function (result) {
                    $(div).html(result); // div -id of your div outside of partial
}
  1. Use full url
return Redirect("http://www.myapp.com");

or return javascript

    return Content("<script>window.location = 'http://www.myapp.com';</script>");

Upvotes: 1

Related Questions