简体   繁体   English

什么是自动生成<Script> tags inside .cshtml?

[英]What is the auto generated <Script> tags inside .cshtml?

I'm trying to learn C# ASP.NET and for that I'm building a project. 我正在尝试学习C#ASP.NET,为此,我正在构建一个项目。 I was wondering what the Script tags inside the .cshtml file is? 我想知道.cshtml文件中的Script标签是什么? For an instance every View page (Create, Edit, Delete etc.) has Script tags. 对于实例,每个“视图”页面(创建,编辑,删除等)都具有脚本标签。

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

I see there is some jQuery included which is a library of JavaScript, but I don't see point of it being there. 我看到其中包含一些jQuery,它是一个JavaScript库,但是我看不到它的意义。

A little snippet of the code which is in the same file as the Script tag above: 与上面的Script标签位于同一文件中的一些代码片段:

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()



<div class="form-horizontal">
    <h4>Car</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

This script tag will call .js files for jquery and put into your html. 该脚本标记将为jquery调用.js文件,并放入您的html中。

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

And you can add script tag in .cshtml There is to way for this 您可以在.cshtml中添加脚本标签

1. Directly added script tag 1.直接添加脚本标签

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

It's will be add this script under the RenderBody() method (RenderBody() method is in the Layout.cshtml file) 将在RenderBody()方法下添加此脚本(RenderBody()方法在Layout.cshtml文件中)

2. You have to add below code into your Layout.cshtml 2.您必须将以下代码添加到Layout.cshtml中

@RenderSection("scripts", required: false)

And you can add you can add your script into your View like this 您可以添加,您可以像这样将脚本添加到视图中

@section scripts {
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}

then this script file will be added to where you put your @RenderSection("scripts", required: false) code. 那么此脚本文件将被添加到您放置@RenderSection(“ scripts”,required:false)代码的位置。

@RenderSection("Name", required: Boolean) code. @RenderSection(“ Name”,必填:布尔值)代码。 Name: Type your name of whatever you want to call in .cshtml Boolean: You can write true or false. 名称:在.cshtml中键入您想调用的名称的名称布尔值:您可以输入true或false。 If true you have to add this section into your every .cshtml file use this Layout.cshtml. 如果为true,则必须将此部分添加到每个.cshtml文件中,请使用此Layout.cshtml。 Else you haven't. 否则你没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在CSHTML页面中处理HTML标记和C#脚本 - Handling html tags and c# script in cshtml page ASP.Net按钮单击事件处理程序不会自动在代码隐藏中生成,而是放置在页面上的脚本标记中 - ASP.Net Button Click Event Handler not auto generated in code-behind, placed in script tags on page instead 在cshtml标签中使用条件 - use condition in cshtml tags 为什么 my.Net Core 应用程序处理 .cshtml 页面上脚本标记中的 js 文件作为 react js 应用程序的一部分? - Why is my .Net Core app processing js files in script tags on .cshtml page as part of the react js application? .cshtml 中的@{} 是什么意思? - What is that @{} mean in .cshtml? &#39;@&#39;在cshtml中的含义是什么意思? - What is the meaning of '@' sign in cshtml? C#Project有自动生成的类,但是自动生成了什么? - C# Project has auto generated classes, but what auto generated them? (函数,全局变量)之间的Visual Studio 2019脚本标记(在cshtml中)转到定义(F12)不起作用 - Visual Studio 2019 script tags (in cshtml) between (function,global variable) go to definition (F12) doesn't work 在cshtml页面内异步加载 - Async loading inside cshtml page 在Razor CSHTML中切换语句 - Switch statement inside Razor CSHTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM