如何在MVC4的局部视图中添加脚本? [英] How to add a script in a partial view in MVC4?
问题描述
这是我的部分视图中的代码
@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><跨度>@Html.EditorFor(model => model.Result)@Html.ValidationMessageFor(model =>model.Result)</span>
}@section 脚本 {@Scripts.Render("~/bundles/jqueryval")}
请注意在我的代码底部,我有一个 @section
,我意识到如果我在那里设置断点,它就不会运行.如果我在 _Layout.cshtml 中移动那一行,它运行良好,但不是这个想法.
如何在部分 razor 视图中告诉 MVC4 我要添加该库?
您可以使用@Scripts.Render("~/Scripts/my-script.js")
用于 .js 文件和 @Styles.Render("~/Content/my-Stylesheet.css")
用于 css 文件.
注意:它也适用于特定的捆绑包更多详细信息 - 'http://www.asp.net/mvc/overview/performance/bundling-and-minification'
它适用于 razor 中的任何子页面,包括部分视图.有关这些助手的用法的更多信息,请使用谷歌
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.
How can I tell to MVC4 in a partial razor view that I want to add that library?
You can use
@Scripts.Render("~/Scripts/my-script.js")
for .js files and @Styles.Render("~/Content/my-Stylesheet.css")
for css files.
Nb: it works for a particular bundle also More details - '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
这篇关于如何在MVC4的局部视图中添加脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!