找到你要的答案

Q:Make Specific cshtml file to have Access to Functionality

Q:具体cshtml文件访问功能

If I want a specific cshtml file (asp.net mvc 4) to have access to specific functionality in javascript or jquery, how should I enable to do it in Visual Studio 2013?

All cshtml file should not have access to all functionality in javascript or jquery.

Thanks!

如果我想要一个明确的cshtml文件(ASP.NET MVC 4)获得在JavaScript和jQuery的特定功能,我如何能够在Visual Studio 2013吗?

所有cshtml文件不应该访问的JavaScript或jQuery的所有功能。

谢谢!

answer1: 回答1:

In your _Layout.cshtml define new section, e.g.

<head>
  <title>@ViewBag.Title</title>
  @RenderSection("HeadContent", false)
</head>

Then in your views you can use that section to specify which js/jQuery to use:

@model YourViewModel
@section HeadContent{
    <script type="text/javascript" src="your.js"></script>
}

You can also group your scripts into partial .cshtml and then include that in HeadContent using @Html.Partial or @Html.RenderPartial.

在你的_layout.cshtml定义新的部分,例如

<head>
  <title>@ViewBag.Title</title>
  @RenderSection("HeadContent", false)
</head>

然后在你的意见你可以利用这段指定要使用的JS、jQuery:

@model YourViewModel
@section HeadContent{
    <script type="text/javascript" src="your.js"></script>
}

你也可以组织你的脚本部分。cshtml然后包括在使用@html.partial或@html.renderpartial headcontent。

c#  asp.net-mvc-4  razor  visual-studio-2013