【问题标题】:Write css for individual page in mvc3 project为 mvc3 项目中的单个页面编写 css
【发布时间】:2011-03-23 14:39:17
【问题描述】:

如果您感到困惑,我在这里做错了对不起。

我想将 Javascript 或 Css 放在我的页面的头部 [在浏览器中呈现]。 当我尝试将它们放入时,我发现他在 Body 内而不是在 head 标签内。

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

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

我想要 MVC 3 项目的解决方案,我的页面是使用 Razor viewenzine 编写的。

【问题讨论】:

    标签: javascript asp.net-mvc razor


    【解决方案1】:

    在布局页面的头部,添加一个

    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
    }
    

    【讨论】:

    • 当我遇到这个错误“未定义部分:'head'”
    • 好吧,您需要在视图中定义该部分。有关更多信息,请参阅我的答案中的网页。请记住,您需要在每个页面中执行此操作。我将更新我的答案,提供有关如何仅针对选定页面执行此操作的信息。
    • 添加代码作为您自己问题的“答案”并不是正确的方法。编辑您的问题并添加它。 :)
    • 对不起,我设法引入了一个现已修复的错字。它应该是@section 头,而不是@section 菜单。对不起。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2014-05-04
      • 1970-01-01
      • 2013-09-12
      • 2016-11-01
      • 2011-11-16
      相关资源
      最近更新 更多