我正在尝试使用 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#Login
和 body#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/