在mvc3项目中为单个页面写css

时间:2010-07-31 10:59:41

标签: javascript asp.net-mvc razor

如果你感到困惑,我在这里说错了。抱歉。

我想将Javascript或Css放在我的页面的头部[谁在浏览器中呈现]。 当我试图把它们放进去的时候,我发现他身体里面没有头部标签。

i know that i can easily put in head if i not inherit them from master page.

但如果我从母版页继承它们,我怎么能放在我的页面上。

我想要一个MVC 3项目的溶剂,我的页面是用Razor viewenzine编写的。

2 个答案:

答案 0 :(得分:8)

在布局页面的head部分中,添加

RenderSection("head")

呼叫。然后,在Razor视图中,添加

@section head {
 @myincludemarkup
}

“myincludemarkup”当然是你的脚本/样式表引用的html标记。

编辑:

布局页面(母版页)中的部分可能如下所示:

<head>
@RenderSection("head")
</head>

这会强制你的每个观点通过在我的答案的顶部写下代码@section等来定义一个名为“head”的部分。

如果您希望视图中的部分是可选的,则可以编写

<head>
@RenderSection("head", optional:true)
</head>

使用此页面作为参考:

http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

编辑:使用您自己的代码:

@inherits System.Web.Mvc.WebViewPage<dynamic>

@using Razor
@using Razor.Models


@{
    View.Title = "Index";
    LayoutPage = "~/Views/Shared/_Layout.cshtml";
}

<h2>@View.Message</h2>

@{
    UserManager.product prod =  UserManager.getUserinfo();
 }
@prod.Price
@prod.Title
@prod.ID
<h2></h2>

@section head {
 @myincludemarkup
}

答案 1 :(得分:1)

在头部放置内容区域。然后,您可以在任何内容页面中添加代码。