简体   繁体   English

如何在ASP.NET MVC C#项目中插入脚本

[英]How to insert scripts in an asp.net mvc c# project

When I create a default mvc asp.net c# project, on _layout.cshtml inside the body tag there is the Render.Body() and before the closing of the body there are the script bundles for jquery and bootstrap. 当我创建默认的mvc asp.net c#项目时,在body标记内的_layout.cshtml上有Render.Body(),在关闭主体之前有用于jquery和bootstrap的脚本包。 When I try to write a script in my View, it says that jQuery is not defined. 当我尝试在View中编写脚本时,它表示未定义jQuery。 That's obvious because the script bundles get called after the Render.Body(). 这很明显,因为脚本包在Render.Body()之后被调用。 So I'm asking the silly question. 所以我问一个愚蠢的问题。 Is it wrong to move the bundles above the Render.Body()? 将包移动到Render.Body()上方是否错误?

You need to add a 'section' to your _Layout file then the View can inject into it. 您需要在_Layout文件中添加一个“节”,然后视图才能插入其中。 This will ensure the script runs after jquery is available. 这将确保脚本 jquery可用运行。

Try this: 尝试这个:

Layout 布局

Add this under the jquery (and other) scripts in there: 在其中的jquery(和其他)脚本下添加以下代码:

@RenderSection("Scripts", required: false)

Views 观看次数

Then in any view you want to add some script do the following: 然后,在要添加一些脚本的任何视图中,执行以下操作:

@section scripts {
    <script>
    //do stuff here
    </script>
}

If your dependency scripts such as jQuery are loaded after those that require them, you need to move them above. 如果像jQuery这样的依赖脚本在需要它们的脚本之后加载,则需要将它们移到上方。 You can do this, as you suggested, by moving the jQuery bundle above the body or, depending on the structure of your application, including the jQuery at the start of your script bundle. 您可以按照建议的方式将jQuery包移到正文上方,或者根据应用程序的结构(包括脚本包开头的jQuery)进行操作。

There are likely other methods to get around this problem but I have not used them. 可能还有其他方法可以解决此问题,但我没有使用过。 It is not 'wrong' to put scripts above the body, it just has different advantages and disadvantages compared to other options. 将脚本放在正文上方并不是“错误”,与其他选项相比,它具有不同的优点和缺点。

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

相关问题 C# ASP.NET MVC 脚本被加载两次 - C# ASP.NET MVC scripts being loaded in twice asp.net项目C#,SQL Server,插入不起作用 - asp.net project C#, SQL server, Insert is not working C#代码:这是ASP.NET AJAX脚本的工作方式吗? - C# Code: Is this how ASP.NET AJAX scripts work? 如何在Asp.net MVC C#中从会话而不是数据库中插入和获取数据 - How to insert and fetch Data from Sessions instead of Database in Asp.net MVC C# C#MVC 4 ASP.net,如何将文件插入数据库存储中(二进制) - C# MVC 4 ASP.net, How to Insert File into Database store in (binary) 如何使用C#动态创建ASP.NET MVC项目? - How to create an ASP.NET MVC project dynamically using C#? 如何在C#ASP.NET MVC项目中使用ajax更新SQL表中的记录顺序? - How to update orders of records in SQL table using ajax in C# ASP.NET MVC project? 如何在 ASP.NET MVC C# 项目的新选项卡中打开 PowerPoint 演示文稿 - How to open PowerPoint Presentation in New Tab in ASP.NET MVC C# project 如何在Visual Studio 2013中编辑ASP.Net MVC C#项目模板文件? - How can I edit ASP.Net MVC C# project template files in Visual Studio 2013? C#ASP.NET MVC项目和Google Maps:如何使用C#List &lt;&gt;中的值填充JavaScript数组 - C# ASP.NET MVC Project and Google Maps: How to fill JavaScript array with values from C# List<>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM