视图中的Styles.Render

3

我有一个布局页面(Layout page),通过MVC4的Style.Render函数渲染了一些样式表。

然而,我想在仅限于某些视图的情况下渲染一些特定于页面的样式表 - 最佳实践是什么?您会创建一个新的bundle并告诉Layout何时进行渲染吗?

2个回答

3
在您的布局中,定义一个用于额外样式的部分:
<html>
    <head>
        <!-- other stuff here -->
        @RenderSection("CustomStyles", required: false);
    </head>
    <!-- rest of layout -->
</html>

在您看来:
@section CustomStyles {
    @* custom style content here *@
}

由于该部分不是必需的,因此您可以将视图特定样式添加到仅需要它的页面中。


1

你可以使用@RenderSection("yourCustomSection")来实现这一点。如果不是必需的,那么你可以可选地进行操作。

@RenderSection("MyCustomStyleSection",false)

现在,您可以将自定义样式包呈现在所需的视图中,如 @section

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接