繁体   English   中英

调用另一个控制器,并对调用的控制器操作MVC ASP.NET使用不同的布局

[英]Call another controller and use a different Layout for the called controller action MVC ASP.NET

是MVC ASP.Net的新手。 我想从另一个控制器调用一个动作方法,并使用另一个布局页面。

我的视图/共享文件夹有两个布局页面:〜/ Views / Shared / _Layout1.cshtml和〜/ Views / Shared / _Layout2.cshtml。 下面是我的代码:

//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();
    }

 }

如果要使用其他布局,则可以使用View方法的重载版本:

return View("view", "_Layout1"); 

如果要将控制流重定向到其他控制器/操作,则:

return RedirectToAction("action", "controller");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM