什么时候必须使用@section脚本? [英] When I have to use @section scripts?
问题描述
我当前的结构具有包含页眉,正文和页脚的布局.在体内加载一个使用ajax的视图,以调用一个动作控制器来返回一个Json并绘制一个树形视图.当用户单击树形视图时,页脚应加载详细信息.但是不起作用,我的猜测是因为脚本部分无法正确呈现.
My current structure has a layout with header, body and footer. Inside the body load a view using ajax to call for a action controller returning a Json and painting a tree view. When user click on the tree view the footer should load the detailed information. But isnt working, my guess is because the scripts section isnt render properly.
现在脚本位于布局中,没有捆绑包或其他任何东西,并且可以在主体上正常工作,因为我使用了Jquery和Tree来加载Json数据.
Right now the script are in the layout without bundles or anything and work ok on the Main body because I use Jquery and a Tree to load the Json data.
但是在局部视图中出现错误.我可以写一个@section scripts
区域,然后从Partial View中的布局复制所有脚本,但是为什么要复制代码?
But in the partial View get an error. I could write a @section scripts
area and copy all the script from the layout in the Partial View but why should I duplicate the code?
最糟糕的是只会给我生产环境中的问题...我的开发环境可以正常工作.
The worst part is only give me problem in the production enviroment ... on my devolpment enviroment works ok.
所以问题:
-
为什么主视图可以看到布局上定义的脚本,但部分视图却看不到?
Why the main view can see the scripts define on the Layout but the Partial View Doesnt?
为什么我的开发环境可以正常工作,但是生产环境却不能正常工作?
Why my development enviroment work ok, but productions doesnt?
我应该怎么做才能解决这个问题?
What should I do to solve this?
更多测试.
这是一个测试视图,在主体中呈现.但是我需要包括脚本部分,否则即使布局也具有脚本,对话框也不会显示.
This is a test View, this render in the Body. But I need include script section otherwise the dialog doesnt show, even when layout have the scripts too.
@{
ViewBag.Title = "TreeDetails";
}
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<h2>TEST PAGE</h2>
<script>
// Your code goes here.
$(document).ready(function () {
console.log("before dialog");
$("#dialog").dialog();
console.log("after dialog");
})
</script>
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
</body>
</html>
@section scripts {
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
}
推荐答案
由于无法识别的jquery函数,似乎出现运行时错误..尝试将链接引用从布局标题中的view @section区域移至jquery.
It seems a run time error due to an unrecognized jquery function.. try to move your link reference to jquery from your view @section area in the layout header..
这篇关于什么时候必须使用@section脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!