[英]Call another controller and use a different Layout for the called controller action MVC ASP.NET
Am new to MVC ASP.Net. 是MVC ASP.Net的新手。 I would like to call an action method from another controller and use a different layout page.
我想从另一个控制器调用一个动作方法,并使用另一个布局页面。
My Views/Shared folder has two layout pages: ~/Views/Shared/_Layout1.cshtml and ~/Views/Shared/_Layout2.cshtml. 我的视图/共享文件夹有两个布局页面:〜/ Views / Shared / _Layout1.cshtml和〜/ Views / Shared / _Layout2.cshtml。 Below is my code:
下面是我的代码:
//HomeController
public class HomeController : Controller
{
public ActionResult Index(string id)
{
if(String.IsNullOrEmpty(id))
{
//Call someMethod from UserController
//And when called, it should a different Layout page: ~/Views/Shared/_Layout2.cshtml
return View("someMethod");
}
else
{
return AboutPage(id); //uses ~/Views/Shared/_Layout1.cshtml
}
}
public ActionResult AboutPage(string id)
{
return View();
}
}
If you want to use different layout then you can use overloaded version of View
method: 如果要使用其他布局,则可以使用
View
方法的重载版本:
return View("view", "_Layout1");
If you want to redirect control flow to different controller/action then: 如果要将控制流重定向到其他控制器/操作,则:
return RedirectToAction("action", "controller");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.