我在布局页面中有一个按钮,它应该在不同的视图之间导航。
<a id="next" href="/Navigation?CurrentPage=@ViewBag.CurrentPage">Next</a>我在每个页面的ViewBag.CurrentPage值中填充ViewModel值。
导航控制器拦截锚在以下控制器中单击-
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按顺序包含ActionNames,因此只需增加currentPageEnum值以查找下一页。
enum PageType
{
Page1,
Page2
}在Global.asax.cs中,每个动作都有一个映射路径,如下所示-
routes.MapRoute("Page1", "Page1", new { controller="controller1", action="Page1"});
routes.MapRoute("Page2", "Page2", new { controller="controller2", action="Page2"});问题:我无法用此代码重定向到其他控制器-
RedirectToAction(nextPageEnum.ToString()); 请求终止而不重定向。
谢谢!
发布于 2012-12-11 12:26:35
添加返回语句并使函数返回一些内容。
public class NavigationController : Controller
{
public ActionResult Index(string CurrentPage)
{
PageType currentPageEnum = (PageType)Enum.Parse(typeof(PageType), CurrentPage);
PageType nextPageEnum = currentPageEnum + 1;
return RedirectToAction(nextPageEnum.ToString());
}
}由于您引用的是映射的路由名称,而不是操作,所以我认为您需要RedirectToRoute而不是RedirectToAction,如下代码所示:
public class NavigationController : Controller
{
public ActionResult Index(string CurrentPage)
{
PageType currentPageEnum = (PageType)Enum.Parse(typeof(PageType), CurrentPage);
PageType nextPageEnum = currentPageEnum + 1;
return RedirectToRoute(nextPageEnum.ToString());
}
}但是,我建议在MVC环境中从(剃刀)视图中导航的最佳方法如下:
<div>
@Html.ActionLink(string linkText, string actionName)
</div>如果动作位于同一个控制器中。如果不使用此重载:
<div>
@Html.ActionLink(string linkText, string actionName, string controllerName)
</div>https://stackoverflow.com/questions/13820399
复制相似问题