我怎么能有视图特定&LT

我怎么能有视图特定&LT

本文介绍了我怎么能有视图特定< HEAD>内容使用Asp.Net MVC 3剃须刀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要到一个特定的样式表中,除了那些已经被在_Layout.cshtml链接某些视图链接。对于非剃须刀,我请参阅使用内容占位符。我怎么会做这样的剃须刀?

I want to link a specific style sheet in certain Views in addition to what already gets linked in _Layout.cshtml. For non-Razor, I see using the content place holder. How would I do this for Razor?

推荐答案

在剃刀内容占位符的等效是部分。

The equivalent of content placeholders in Razor are sections.

在您_Layout.cshtml:

In your _Layout.cshtml:

<head>
@RenderSection("Styles", required: false)
</head>

然后在您的内容页:

Then in your content page:

@section Styles {
    <link href="@Url.Content("~/Content/StandardSize.css")" />
}


这是另一种解决办法是把你的风格融ViewBag / ViewData的:


An alternative solution would be to put your styles into ViewBag/ViewData:

在您_Layout.cshtml:

In your _Layout.cshtml:

<head>
    @foreach(string style in ViewBag.Styles ?? new string[0]) {
        <link href="@Url.Content(style)" />
    }
</head>

而在你的页面内容:

And in your content page:

@{
    ViewBag.Styles = new[] { "~/Content/StandardSize.css" };
}

这工作,因为该视图页面获取布局之前执行。

This works because the view page gets executed before the layout.

这篇关于我怎么能有视图特定&LT; HEAD&GT;内容使用Asp.Net MVC 3剃须刀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 18:38