![](/img/trans.png)
[英]Passing data between two ActionResults in the same controller rendering different views in asp.net mvc
[英]Return different views same controller in ASP.NET MVC
我想将用户发送到两个不同页面中的一个,具体取决于isCustomerEligible
的值。 当该变量的值设置为false时,它会调用Index但后面会返回Customer
的视图而不是Index
的视图。
public ViewResult Index()
{
return View();
}
public ViewResult Customer()
{
DetermineCustomerCode();
DetermineIfCustomerIsEligible();
return isCustomerEligible ? View() : Index();
}
如果您只返回View(),它将查找与您的操作同名的视图。 如果要指定返回的视图,则必须将视图的名称作为参数。
public ViewResult Customer()
{
DetermineCustomerCode();
DetermineIfCustomerIsEligible();
return isCustomerEligible ? View() : View("Index");
}
如果您想要实际触发Index事件而不仅仅返回其视图,则必须返回RedirectToAction()并将返回类型更改为ActionResult
public ActionResult Customer()
{
DetermineCustomerCode();
DetermineIfCustomerIsEligible();
return isCustomerEligible ? View() : RedirectToAction("Index");
}
您需要做的就是返回所需的视图。
如果要返回与您所处的操作同名的View,只需使用return View();
如果您希望返回与您所处的操作方法不同的视图,则指定视图的名称,如此return View("Index");
public ViewResult Index()
{
return View();
}
public ViewResult Customer()
{
DetermineCustomerCode();
DetermineIfCustomerIsEligible();
return isCustomerEligible ? View() : View("Index");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.