I have a button in Layout page, which is supposed to navigate between different views.
<a id="next" href="/Navigation?CurrentPage=@ViewBag.CurrentPage">Next</a>
I populate ViewBag.CurrentPage value in ViewModel of each page.
Navigation controller intercepts anchor click in following controller –
public class NavigationController : Controller
{
public void Index(string CurrentPage)
{
PageType currentPageEnum = (PageType)Enum.Parse(typeof(PageType), CurrentPage);
PageType nextPageEnum = currentPageEnum + 1;
RedirectToAction(nextPageEnum.ToString());
}
}
Enum contains ActionNames in sequential order, so just increment currentPageEnum value to find next page.
enum PageType
{
Page1,
Page2
}
Each action has a mapping route in Global.asax.cs as below –
routes.MapRoute("Page1", "Page1", new { controller="controller1", action="Page1"});
routes.MapRoute("Page2", "Page2", new { controller="controller2", action="Page2"});
Question:
I have not been able to redirect to other controllers with this code-
RedirectToAction(nextPageEnum.ToString());
Request terminates without redirect.
- What info am I missing.
- Is there a better efficient way to navigate
between diffrent views, in ASP MVC
Thanks!
Add a return statement and make the function return something.
And since you refer to a mapped route name and not an action I believe you need
RedirectToRouteinstead ofRedirectToActionlike in this code:But I would suggest that the best way to navigate in an MVC environment from the (razor)view is like this:
If the action is in the same controller. If not use this overload: