html - 使用 CSS 为 ASP.NET MVC 中的每个 View 设置不同的背景颜色

标签 html css asp.net-mvc background-color

我正在尝试使用 ASP.NET MVC 4 构建网站。我的元素带有一个 css 文件,它会影响我使用主布局 (_Layout.cshtml) 的所有 View 。我希望能够根据 View 使用三种不同背景颜色中的一种,我需要帮助。这是我目前所拥有的:

/* main layout
----------------------------------------------------------*/

.content-wrapper {
    margin: 0 auto;
    max-width: 960px;
}

#body {
    background-color: #eb6f5c;
    clear: both;
    padding-bottom: 35px;
}

#body#Geldi {
    background-color: #a0f3ab;
    clear: both;
    padding-bottom: 35px;
}

#body#Login {
    background-color: #e8f3a0;
    clear: both;
    padding-bottom: 35px;
}


footer {
    clear: both;
    background-color: #e2e2e2;
    font-size: .8em;
    height: 100px;
}

研究并没有让我比这更进一步。我所有的页面仍然是粉红色 (#eb6f5c)。我如何告诉 Visual Studio 我想为特定 View 使用“Geldi”主体类型或“登录”主体类型?

最佳答案

首先,我认为您不能使用那个特定的 CSS 选择器(我认为它转换为选择 ID="body"和 ID="Login"(或“Geldi”)的元素),但 CSS 不是我的力量)。如果您将它设置为页面的 body 标记,请去掉 #body 并只使用 body,因为您可以通过 HTML 元素名称进行选择。所以改为:body#Loginbody#Geldi

在那之后,最好使用 ViewData,并将其设置在您的 subview (登录 View 或其他 View )中,这将使其对您的主视图可用

例如:

_Layout.cshtml

<body id='@(ViewData["PageId"] ?? "")'>
    @RenderBody()
</body>

登录.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewData["PageId"] = "Login";
}

<div id='content'>
    <!-- All of your page content here and whatever -->
</div>

关于html - 使用 CSS 为 ASP.NET MVC 中的每个 View 设置不同的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11732022/

相关文章:

html - 全宽 - Owl Carousel 2 期

asp.net-mvc - Orchard,基于插件的架构,设计模式

html - 边距顶部和框阴影在 tbody 中不起作用

html - div的CSS变换中心

javascript - 下拉弹跳问题

javascript - iOS 上的 Safari 在滚动停止后仅显示固定元素

html - CSS:在不使用表格的情况下显示尽可能窄的图像列

html - Ctrl + PLUS/MINUS 在现代浏览器中有什么作用?

asp.net-mvc - SignalR 跨域 : errors with not allowed by Access-Control-Allow-Origin

c# - Entity Framework 到 MySql 连接到数据库失败