how to redirect to another action in same controller?

24,282

Solution 1

return RedirectToAction("ActionName");

Instead of return Redirect("/Elearning/NotAuthorized"); do this:

return RedirectToAction("NotAuthorized"); // if you're inside the Elearning controller

or

RedirectToAction("NotAuthorized", "Elearning"); // if calling from other controller

Solution 2

Try

return RedirectToAction("SomeAction");

Solution 3

If you want to return a redirect

return RedirectToAction("NotAuthorized");

is the valid way to do this. Make sure that your method actually exists.

Alternatively, if you don't want a redirect

return View("NotAuthorized");

works as well.

Share:
24,282
dinesh
Author by

dinesh

Updated on April 17, 2020

Comments

  • dinesh
    dinesh about 4 years

    i want to redirect to another action in same controller. how can we achieve this? i tried like return

    RedirectToAction("NotAuthorized");

  • dinesh
    dinesh about 12 years
    return RedirectToAction("NotAuthorized"); public ActionResult NotAuthorized() { int userId = GetCurrentUserID(); this is how i write in contoller.. ViewBag.ViewList = _menuViewUtility.GetViewList(userId); ViewBag.MenuList = _menuViewUtility.GetMenuList(userId, ViewSession.ViewId); return View(); }
  • itsmatt
    itsmatt about 12 years
    What fails here? Does the View that you are trying to return actually exist?
  • dinesh
    dinesh about 12 years
    ya view also exist i can go to that view
  • itsmatt
    itsmatt about 12 years
    What's the error here? We'd need to know the error to help you.
  • dinesh
    dinesh about 12 years
    i tried by putting debugger after excuting that statement it is comming back to previous view only
  • dinesh
    dinesh about 12 years
    if (so.Count == 0) { return Redirect("/Elearning/NotAuthorized"); }
  • dinesh
    dinesh about 12 years
    public ActionResult NotAuthorized() { int userId = GetCurrentUserID(); ViewBag.ViewList = _menuViewUtility.GetViewList(userId); ViewBag.MenuList = _menuViewUtility.GetMenuList(userId, ViewSession.ViewId); return View(); }
  • dinesh
    dinesh about 12 years
    @{ ViewBag.Title = "NotAuthorized"; } <script type="text/javascript"> dojo.require("dojo.parser"); dojo.require("dijit.form.Button"); </script> <label>You are not authorized to access the selected resource. </label><br /> <a href="#" onClick="GoBack();"></a> <script type="text/javascript"> function GoBack() { var Url = @Request.Form["RedirectUrl"]; window.location(Url); } </script>