相关疑难解决方法(0)

从视图或部分视图添加CSS或JavaScript文件到布局头

布局页面头:

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

应用程序需要的View(AnotherView):

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

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

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

问题:我们如何这些CSS文件链接添加到Layouthead和AnotherPartial链接到Layout头

RenderSection不是一个好主意,因为AnotherPage可以有多个Partials.添加所有CSS到头部是没用的,因为它将动态更改(它取决于Anotherpages).

asp.net-mvc razor asp.net-mvc-3

171
推荐指数
6
解决办法
22万
查看次数

JQuery After Body ASP .NET MVC 4

我刚刚创建了一个ASP .NET MVC 4 WebAPI项目.

在查看默认的_Layout.cshtml时,我看到在渲染正文(不在头部)之后插入了jquery脚本.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/themes/base/css", "~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这会导致错误

$ is not defined 
Run Code Online (Sandbox Code Playgroud)

当然,试着写

$(document).ready(function() {...}).
Run Code Online (Sandbox Code Playgroud)

移动

@Scripts.Render("~/bundles/jquery")
Run Code Online (Sandbox Code Playgroud)

进入_Layout.cshtml的head部分纠正问题,jquery按预期工作.

这是怎么回事?我做错了什么,并且_Layout.cshtml中的Script.Render的默认位置是有原因的吗?

asp.net-mvc jquery asp.net-mvc-4

18
推荐指数
1
解决办法
1万
查看次数