如何嵌入的JavaScript在Asp.net MVC视图 [英] how to embed javascript in Asp.net MVC view
问题描述
我怎样才能插入JavaScript在asp.net MVC应用程序(视图)?
How can I insert javascript in asp.net MVC application(view)?
我做下面的事情在我的javascript:
I am doing following things in my javascript:
1)从DOM元素读值。
1) Read values from DOM elements.
2)jQuery的阿贾克斯发送数据的Controler操作方法。
2) Send data to Controler action method from Jquery Ajax.
3)验证我的输入。
4)读/写的cookies
4) Read/Write cookies
我怎样才能在视图中插入的JavaScript。
How can I insert javascript in View.
它应该是这样的?
<script src="../Scripts/Jquery-2.1.js" type="text/javascript"></script>
<script src="../Scripts/jquery.cookie.js" type="text/javascript"></script>
<script>
$(document).ready(function ()
{
$.cookie("pc", "Navada, US");
}
);
</script>
@{
ViewBag.Title = "ViewPage1";
}
<h2>ViewPage1</h2>
谢谢您的回答
推荐答案
不知道如果我明白你的问题的权利,但如果是这样,你可能需要把你的脚本标记插入头标记。
Not sure if i understand your question right, but if so you may need to put your script tags into the head tag.
要能够把你的自定义JS的东西变成你可以尝试在看这个例子头:
To be able to put your custom js things into head you could try looking at this example:
在您的网页_layout添加此之间&LT; HEAD&GT;&LT; /头&GT;
In your _Layout page add this between <head></head>:
@RenderSection("CustomScripts", required: false);
和您的网页上:
@section CustomScripts
{
<script src="@Url.Content("~/Scripts/foo.js")" type="text/javascript"></script>
}
编辑:作为@Liam正确地指出,_layout文件可以在其他任何路径,@应用StephenMuecke的澄清
as @Liam correctly pointed out that the _Layout file could be in any other path, @StephenMuecke's clarification applied.
这篇关于如何嵌入的JavaScript在Asp.net MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!