js文件加载不偏后刷新视图 [英] Js file not loaded after partial view is refreshed

查看:191
本文介绍了js文件加载不偏后刷新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的局部视图:

<div class="well">
    <h3>
        <strong>@Model.Name</strong>
        <span class="pull-right label label- primary">@Model.AverageRaiting.ToString("# stars")</span>
    </h3>
    <span class="lead">@Model.Description</span>
    @Html.DialogFormLink("Update", Url.Action("UpdatePhoto", new {id = @Model.PhotoId}), "Update Photo", Url.Action("Photo"))
    @Html.Action("InitializeAlerts")
</div>
@section scripts{
    <script src="~/Scripts/reload.alerts.js"></script>
}

一切是在第一时间确定。但经过我更换局部视图由另一人喜欢自定义脚本上面reload.alerts.js不起作用。这个脚本是 @ Html.Action(InitializeAlerts)的行动。
InitializeAlerts 方法只返回一个名为_Alert的局部视图。
我该怎么做才能解决这个问题?
谢谢你。

Everything is ok by the first time. But after I replace the partial view by another one like above the custom script "reload.alerts.js" doesn't work. This script is for @Html.Action("InitializeAlerts") action. InitializeAlerts method just return a partial view named _Alert. What shall I do to fix this problem? Thanks.

推荐答案

您不应该在局部视图使用 @section脚本

You shouldn't use @section scripts in Partial Views.

一切工作正常,如果你呈现在主视图becouse剃刀知道在哪里把你的脚本上 _layout 但是当你刷新,如果你基本上让Ajax调用,你得到的行HTML

Everything works fine if you render it in Main View becouse Razor knows where to put your script on _Layout but when you refresh if you basically make ajax call and you get row html.

既然你只能得到部分页面而不 _layout 剃刀刚切此行,你的脚本链接甚至不来的客户。

Since you get only Partial page without _Layout Razor just cut this line so your script link doesn't even came to client.

您有2个选项:


  1. 像@Alorika说你应该把你的脚本在主视图。但也可能有问题,这取决于你的脚本做什么,它是如何做到这一点。这是很好的做法,我建议使用它。如果脚本不工作,你应该看看它在那里下降。

  2. 删除 @section脚本并把你的脚本链接离不开它。我不知道,但我怀疑有一些可能性时,这取决于你如何写你的脚本将无法工作或者这种方式。

  1. like @Alorika said you should place your script in main View. But there could be problems and it depends on what your script do and how it do it. That's good practice and i recommend to use it. If script doesn't work you should look at it where it falls.
  2. Delete @section scripts and put your script link without it. I'm not sure, but i suspect that there is some possibilities when your script won't work this way either depending on how you write it.

这篇关于js文件加载不偏后刷新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆