如何在MVC4的部分视图中添加脚本?

时间:2023-01-19 16:12:04

This is the code which I have in my partial view

这是部分视图中的代码

@model Contoso.MvcApplication.Models.Exercises.AbsoluteArithmetic

@using(Html.BeginForm())
{
<div>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">@Model.Number1</span>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">+</span>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">@Model.Number2</span>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">=</span>
    <span>
            @Html.EditorFor(model => model.Result)
            @Html.ValidationMessageFor(model => model.Result)
    </span>
</div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Please note at the bottom of my code, I've got a @section, and I realized that it's not running if I set a breakpoint there. If I move that line in the _Layout.cshtml it works well, but that's not the idea.

请注意,在我的代码的底部,我有一个@section,我意识到如果我在那里设置断点,它就不会运行。如果我在_Layout中移动这条线。cshtml它工作得很好,但这不是想法。

How can I tell to MVC4 in a partial razor view that I want to add that library?

如何在部分razor视图中告诉MVC4我要添加那个库?

7 个解决方案

#1


10  

You can use @Scripts.Render("~/Scripts/my-script.js") for .js files and @Styles.Render("~/Content/my-Stylesheet.css") for css files.

您可以对.js文件使用@Scripts.Render(“~/Scripts/my-script.js”),对css文件使用@Styles.Render(“~/Content/my-Stylesheet.css”)。

Nb: it works for a particular bundle also More details - 'http://www.asp.net/mvc/overview/performance/bundling-and-minification'

Nb:它适用于特定的包,也适用于更多的细节——“http://www.asp.net/mvc/overview/performance/bundling-and-minification”

it works on any sub-pages in razor including partial views. for more info google for the usage of these helpers

它适用于razor中的任何子页面,包括部分视图。有关这些帮助程序的用法的更多信息,请参阅谷歌

#2


9  

You can't render layout sections from a partial. Move the section definition to the parent page or layout.

您不能从局部显示布局部分。将section定义移动到父页面或布局。

#3


5  

Check out my answer How to render a Section in a Partial View, which allows you to define Scripts and Styles in any view/partial view. It also takes care of duplicate includes.

请查看我的答案,如何在分部视图中呈现部分,它允许您在任何视图/分部视图中定义脚本和样式。它还负责复制包括。

My personal take is that sections aren't a good solution for styles and javascript includes.

我个人认为,对于样式和javascript包含的部分不是很好的解决方案。

#4


3  

There is no common solution for this issue but you can do the following simplest ways:

这个问题没有通用的解决方案,但你可以用以下最简单的方法:

1) You can create a set of extension method as the following:

1)可创建一套扩展方法如下:

https://*.com/a/6671766/5208058

https://*.com/a/6671766/5208058

2) Simply move your javascript codes into a separated partial view and on your main view, render 2 partial views. One for the main partial view and the other for your scripts as the following:

只需将javascript代码移动到一个独立的部分视图中,在主视图中呈现两个部分视图。一种用于主要部分视图,另一种用于脚本,如下所示:

{
    // markup and razor code for Main View here

    @Html.Partial("Main_PartialView")
}

@section Scripts
{
    @Html.Partial("JavaScript_PartialView")
}

Hope it helps.

希望它可以帮助。

#5


2  

You can add the script directly at the end of the partial view html, without script section (because script section is not rendered in partial views)

您可以直接在部分视图html的末尾添加脚本,而不使用脚本部分(因为脚本部分视图中没有显示脚本部分)

<script language="javascript">
   // Your scripts here
   // ....
</script>

#6


1  

This worked for me allowing me to colocate JavaScript and HTML for partial view in same file for ease of readability

这使我能够将JavaScript和HTML的部分视图放在同一个文件中,便于阅读

In View which uses Partial View called "_MyPartialView.cshtml"

在视图中使用名为“_MyPartialView.cshtml”的部分视图

<div>
    @Html.Partial("_MyPartialView",< model for partial view>,
            new ViewDataDictionary { { "Region", "HTMLSection" } } })
</div>

@section scripts{

    @Html.Partial("_MyPartialView",<model for partial view>, 
                  new ViewDataDictionary { { "Region", "ScriptSection" } })

 }

In Partial View file

在局部视图文件

@model SomeType

@{
    var region = ViewData["Region"] as string;
}

@if (region == "HTMLSection")
{


}

@if (region == "ScriptSection")
{
        <script type="text/javascript">
    </script">
}

#7


0  

This * page provided a full solution to this question: Using sections in Editor/Display templates

这个*页面为这个问题提供了一个完整的解决方案:使用编辑器/显示模板中的部分

TL;DR: Just add the Forloop.HtmlHelpers nuget package https://www.nuget.org/packages/Forloop.HtmlHelpers/ to your project to allow you to run Javascript from Razor Partial Views and Templates in ASP.NET MVC. I have personally used this with my MVC 5 project.

TL;DR:添加Forloop就可以了。htmlhelper nuget包https://www.nuget.org/packages/Forloop.HtmlHelpers/到您的项目,允许您在ASP中的Razor部分视图和模板中运行Javascript。净MVC。我个人在我的MVC 5项目中使用了这个。

#1


10  

You can use @Scripts.Render("~/Scripts/my-script.js") for .js files and @Styles.Render("~/Content/my-Stylesheet.css") for css files.

您可以对.js文件使用@Scripts.Render(“~/Scripts/my-script.js”),对css文件使用@Styles.Render(“~/Content/my-Stylesheet.css”)。

Nb: it works for a particular bundle also More details - 'http://www.asp.net/mvc/overview/performance/bundling-and-minification'

Nb:它适用于特定的包,也适用于更多的细节——“http://www.asp.net/mvc/overview/performance/bundling-and-minification”

it works on any sub-pages in razor including partial views. for more info google for the usage of these helpers

它适用于razor中的任何子页面,包括部分视图。有关这些帮助程序的用法的更多信息,请参阅谷歌

#2


9  

You can't render layout sections from a partial. Move the section definition to the parent page or layout.

您不能从局部显示布局部分。将section定义移动到父页面或布局。

#3


5  

Check out my answer How to render a Section in a Partial View, which allows you to define Scripts and Styles in any view/partial view. It also takes care of duplicate includes.

请查看我的答案,如何在分部视图中呈现部分,它允许您在任何视图/分部视图中定义脚本和样式。它还负责复制包括。

My personal take is that sections aren't a good solution for styles and javascript includes.

我个人认为,对于样式和javascript包含的部分不是很好的解决方案。

#4


3  

There is no common solution for this issue but you can do the following simplest ways:

这个问题没有通用的解决方案,但你可以用以下最简单的方法:

1) You can create a set of extension method as the following:

1)可创建一套扩展方法如下:

https://*.com/a/6671766/5208058

https://*.com/a/6671766/5208058

2) Simply move your javascript codes into a separated partial view and on your main view, render 2 partial views. One for the main partial view and the other for your scripts as the following:

只需将javascript代码移动到一个独立的部分视图中,在主视图中呈现两个部分视图。一种用于主要部分视图,另一种用于脚本,如下所示:

{
    // markup and razor code for Main View here

    @Html.Partial("Main_PartialView")
}

@section Scripts
{
    @Html.Partial("JavaScript_PartialView")
}

Hope it helps.

希望它可以帮助。

#5


2  

You can add the script directly at the end of the partial view html, without script section (because script section is not rendered in partial views)

您可以直接在部分视图html的末尾添加脚本,而不使用脚本部分(因为脚本部分视图中没有显示脚本部分)

<script language="javascript">
   // Your scripts here
   // ....
</script>

#6


1  

This worked for me allowing me to colocate JavaScript and HTML for partial view in same file for ease of readability

这使我能够将JavaScript和HTML的部分视图放在同一个文件中,便于阅读

In View which uses Partial View called "_MyPartialView.cshtml"

在视图中使用名为“_MyPartialView.cshtml”的部分视图

<div>
    @Html.Partial("_MyPartialView",< model for partial view>,
            new ViewDataDictionary { { "Region", "HTMLSection" } } })
</div>

@section scripts{

    @Html.Partial("_MyPartialView",<model for partial view>, 
                  new ViewDataDictionary { { "Region", "ScriptSection" } })

 }

In Partial View file

在局部视图文件

@model SomeType

@{
    var region = ViewData["Region"] as string;
}

@if (region == "HTMLSection")
{


}

@if (region == "ScriptSection")
{
        <script type="text/javascript">
    </script">
}

#7


0  

This * page provided a full solution to this question: Using sections in Editor/Display templates

这个*页面为这个问题提供了一个完整的解决方案:使用编辑器/显示模板中的部分

TL;DR: Just add the Forloop.HtmlHelpers nuget package https://www.nuget.org/packages/Forloop.HtmlHelpers/ to your project to allow you to run Javascript from Razor Partial Views and Templates in ASP.NET MVC. I have personally used this with my MVC 5 project.

TL;DR:添加Forloop就可以了。htmlhelper nuget包https://www.nuget.org/packages/Forloop.HtmlHelpers/到您的项目,允许您在ASP中的Razor部分视图和模板中运行Javascript。净MVC。我个人在我的MVC 5项目中使用了这个。