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.
Author by
dinesh
Updated on April 17, 2020Comments
-
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 about 12 yearsreturn 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 about 12 yearsWhat fails here? Does the View that you are trying to return actually exist?
-
dinesh about 12 yearsya view also exist i can go to that view
-
itsmatt about 12 yearsWhat's the error here? We'd need to know the error to help you.
-
dinesh about 12 yearsi tried by putting debugger after excuting that statement it is comming back to previous view only
-
dinesh about 12 yearsif (so.Count == 0) { return Redirect("/Elearning/NotAuthorized"); }
-
dinesh about 12 yearspublic ActionResult NotAuthorized() { int userId = GetCurrentUserID(); ViewBag.ViewList = _menuViewUtility.GetViewList(userId); ViewBag.MenuList = _menuViewUtility.GetMenuList(userId, ViewSession.ViewId); return View(); }
-
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>