CSS或JavaScript文件从视图或局部视图添加到布局头 [英] Add CSS or JavaScript files to layout head from views or partial views

查看:100
本文介绍了CSS或JavaScript文件从视图或局部视图添加到布局头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

布局页头:

<head>
    <link href="@Url.Content("~/Content/themes/base/Site.css")"
          rel="stylesheet" type="text/css" />
</head>

从应用需求的视图(AnotherView):

A View (AnotherView) from the application needs:

<link href="@Url.Content("~/Content/themes/base/AnotherPage.css")"
      rel="stylesheet" type="text/css" />

和AnotherView有哪些需要一个局部视图(AnotherPartial):

and AnotherView has a partial view (AnotherPartial) which needs:

<link href="@Url.Content("~/Content/themes/base/AnotherPartial.css")"
      rel="stylesheet" type="text/css" />

问:我们如何添加这些CSS文件的链接 AnotherView和AnotherPartial链接布局头部

Question: How can we add these CSS files links AnotherView and AnotherPartial links to Layout head?

RenderSection是不是一个好主意,因为AnotherPage可以有不止一个局部模板。加入所有的CSS头部是没有用的,因为它会dynamicaly(这取决于Anotherpages)。

RenderSection is not a good idea because AnotherPage can have more than one Partials. Add all CSS to head is not useful because it will change dynamicaly (it depends on Anotherpages).

推荐答案

布局:

<html>
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title</title>
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/modernizr-2.0.6-development-only.js")" type="text/javascript"></script>
        @if (IsSectionDefined("AddToHead"))
        {
            @RenderSection("AddToHead", required: false)
        }

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

查看:

@model ProjectsExt.Models.DirectoryObject

@section AddToHead{
    <link href="@Url.Content("~/Content/Upload.css")" rel="stylesheet" type="text/css" />
}

这篇关于CSS或JavaScript文件从视图或局部视图添加到布局头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆