繁体   English   中英

ASP.NET MVC 5-将用户选择的样式表传递给_Layout.cshtml

[英]ASP.NET MVC 5 - Passing user chosen stylesheet to _Layout.cshtml

我创建了一个“设置”页面,用户可以在其中从下拉列表中选择Bootswatch主题。 CSS主题文件位于~/Content/themes 相应的SettingsController将这个主题选择的名称存储在数据库中。 如何使用 @Scripts.Render("~/Content/themes" + themeName) 将值加载到_Layout视图中 这样就可以在每个页面上加载主题。

我为_Layout.cshtml没有控制器以及如何从db中获取数据(即使用模型或控制器,或完全使用其他方法)而苦恼。

这是简化的结构概述:

Website
    Areas
        User
            Controllers
                >SettingsController.cs //here I store the value in the db
            Views
                Settings
                    >Index.cshtml //settings page view
            >_ViewStart.cshtml
    Controllers
    Views
        Shared
            >_Layout.cshtml
        >_ViewStart.cshtml

您可以从数据库中读取当前的“选定布局”,然后根据需要选择“布局”。 你可以这样想:

switch (SystemInfo.Theme)
        {
            case 1:
                Layout = "~/Views/Shared/Layouts/Themes/Blue/_LayoutMain.cshtml";
                break;
            case 2:
                Layout = "~/Views/Shared/Layouts/Themes/nice/_LayoutMain.cshtml";
                break;
           default:

            Layout = "~/Views/Shared/Layouts/Themes/default/_LayoutMain.cshtml";
            }

您可以将此代码放在_viewstart而无需设置“ Layout ”。

暂无
暂无

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

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