根据控制器指定不同的_Layout.cshtml

发布于 2024-10-31 01:04:12 字数 205 浏览 1 评论 0原文

我创建了一个 asp mvc3 项目,我想要根据选择的控制器有不同的 _Layout.cshtml 。这是因为控制器 1 有 2 个按钮,控制器 2 有 3 个按钮,控制器 3 有 4 个按钮。每个控制器适用于特定类型的用户,因此取决于登录。

我如何将控制器及其视图链接到另一个 Layout.cshtml,现在有一个布局,它位于 Shared 文件夹下。

谢谢!

I created an asp mvc3 project, I want to have a different _Layout.cshtml depending on which controller is selected. This is because with controller 1 it has 2 buttons with the controller2 there will be 3 and with the controller3 there will be 4. Each controller is for a specific type of user, so it depends on the login.

How can i link a controller and its views to another Layout.cshtml, right now there is one layout and it's under the Shared folder.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-11-07 01:04:12

视图应该决定布局,而不是控制器。

控制器应该只确定返回什么视图。

然后,您可以在视图顶部指定布局。
您可以在其周围添加 If 语句以根据您的数据更改它

@{
    if(ViewBag.someValue)
       Layout = "~/Views/Shared/_Layout.cshtml";
    else
        Layout = "~/Views/Shared/_otherLayout.cshtml";
}

The View should determine the layout, not the controller.

The Controller should just determine what View is returned.

Then in the top of your view you can specify the layout.
You could add a If statement around it to change it based on your data

@{
    if(ViewBag.someValue)
       Layout = "~/Views/Shared/_Layout.cshtml";
    else
        Layout = "~/Views/Shared/_otherLayout.cshtml";
}
微暖i 2024-11-07 01:04:12

此时,由于另一个有点过时并且使用 mvc 5 ,我知道您会遇到一些没有括号的问题。 如果您希望使用视图来执行逻辑,那么这里有一个更完整的答案。

控制器

public ActionResult Index()
{
    ViewBag.Admin = 1;
    return View();
}

视图

@{

    if (ViewBag.Admin == 1)
    {
        Layout = "~/Views/Shared/_AdminLayout.cshtml";
    }
    else
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

}

At this point since the other one is a bit dated and with mvc 5 , I know you will have some issues with not having brackets. If you wish to use the View to be doing logic then here is a more complete answer.

Controller

public ActionResult Index()
{
    ViewBag.Admin = 1;
    return View();
}

View

@{

    if (ViewBag.Admin == 1)
    {
        Layout = "~/Views/Shared/_AdminLayout.cshtml";
    }
    else
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文