[英]How to run javascript function from MVC View?
I have tried everything what I have found on the Internet, but nothing worked.我已经尝试了我在互联网上找到的所有内容,但没有任何效果。 I have an external javascript file called calendarCZ.js, the relative path is: ~/Scripts/calendarCZ.js.
我有一个名为 calendarCZ.js 的外部 javascript 文件,相对路径是:~/Scripts/calendarCZ.js。
I want this js file to include to my View called getSideBar.cshtml.我希望这个 js 文件包含到我的名为 getSideBar.cshtml 的视图中。
I have written this in Layout.cshtml:我在 Layout.cshtml 中写了这个:
@Scripts.Render("~/Scripts/calendarCZ.js")
@RenderSection("Scripts", required: false)
I have written this in getSideBar.cshtml:我在 getSideBar.cshtml 中写了这个:
Scripts.Render("~/Scripts/calendarCZ.js")
@RenderSection("Scripts", required: false)
@section Scripts{
<script src="@Url.Content("~/Scripts/calendarCZ.js")">
@Scripts.Render("~/Scripts/calendarCZ.js")
@if (ViewBag.JS == true)
{
printCalendarCZ(@Model.events);
}
</script>
}
And he is still ignoring my script with printCalendarCZ(events) function.而且他仍然使用 printCalendarCZ(events) 函数忽略我的脚本。 Error: Identifier printCalendarCZ does not exist in the current context.
错误:标识符 printCalendarCZ 在当前上下文中不存在。
Does anyone really know how to include the javascript in MVC 5?有谁真的知道如何在 MVC 5 中包含 javascript?
Thanks for replies.感谢您的回复。
Petr彼得
try directly using script tag尝试直接使用脚本标签
<script src="@Url.Content("~/Scripts/calendarCZ.js")"></script>
or或者
//take the relative path
<script src="../Scripts/calendarCZ.js" type="text/javascript"></script>
There are two way to do this.有两种方法可以做到这一点。
@Scripts.Render("~/Scripts/calendarCZ.js")
<script type="text/javascript">
$(function () {
@if (ViewBag.JS == true)
{
@:printCalendarCZ(@Model.events);
}
});
</script>
or或者
@Scripts.Render("~/Scripts/calendarCZ.js")
<script type="text/javascript">
$(function () {
var jsValue = @ViewBag.JS;
if (jsValue == true) {
printCalendarCZ(@Model.events);
}
});
</script>
and don't forget to include jquery.并且不要忘记包含jquery。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.