JavaScript 无法在部分视图中工作
此问题类似于 在内部执行 Javascript 中描述的问题ASP.NET MVC 中的部分视图
下面的index.cshtml 中的代码工作正常...
<label for="locationOfSearch"> in :</label> @Html.TextBox("locationOfSearch")
<input type="submit" value="Search" style="background-color:Green"/>
@section JavaScript {
<script type="text/javascript">
$(document).ready(function () {
$("#locationOfSearch").autocomplete({
source: '@Url.Action("AutocompleteAsyncLocations")'
})
});
</script>
}
但是当我将上面的代码和相应的脚本文件复制并粘贴到另一个视图中,然后粘贴到index.cshtml 中时。 cshtml 如果我打电话Html.Partial(新视图名称),自动完成不起作用...
请让我知道如何解决它而无需太多修改...
This problem is similar to what is described in Execute Javascript inside a partial view in ASP.NET MVC
The below piece of code in index.cshtml is working fine...
<label for="locationOfSearch"> in :</label> @Html.TextBox("locationOfSearch")
<input type="submit" value="Search" style="background-color:Green"/>
@section JavaScript {
<script type="text/javascript">
$(document).ready(function () {
$("#locationOfSearch").autocomplete({
source: '@Url.Action("AutocompleteAsyncLocations")'
})
});
</script>
}
But when I copy and paste the above code and the respective script files to a another view and then in index.cshtml if I call Html.Partial(new view name), Autocomplete is not working...
Kindly let me know how I solve it without much modification...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在局部视图中使用剖面。它们根本不起作用。因此,您必须将
@section JavaScript
保留在视图中,以便注册脚本,然后渲染仅包含标记的部分。您还可以编写自定义帮助器方法来实现此目的,如 此答案< /a>.You cannot use sections in partial views. They simply don't work. So you will have to keep the
@section JavaScript
in the view in order to register scripts and then render the partial which will contain only the markup. You could also write custom helper methods to achieve this as shown in this answer.部分视图需要引用所有脚本,即使您已经在母版/布局页面中引用了它。创建一个部分视图(例如:_Scripts.cshtml)并将所有脚本和样式表引用放入其中。然后在每个视图中调用这个部分视图:
Partial views need to have a reference to all scripts, even though you've already referenced it in the master/layout page. Create a partial view (ex: _Scripts.cshtml) and put all of your scripts and stylesheet references in it. Then call this partial view in every view: