[英]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.