简体   繁体   English

ASP.NET MVC5:实时加载的部分视图不会加载动态Javascript参考

[英]ASP.NET MVC5: live loaded partial view doesn't load dynamic Javascript reference

My goal is to refresh a partial View containing an interactive world map. 我的目标是刷新包含交互式世界地图的局部视图。 I have a Controller action 我有一个控制器动作

public JavaScriptResult DynMap(int id, List<int> relevantCountries = null){}

which returns the map data as JavaScriptResult . 它将地图数据作为JavaScriptResult返回。

I call this Javascript in my Partial View "MapSite" with 我在我的部分视图中将此Javascript称为“ MapSite”,

<script src="/JS/DynMap/@ViewBag.GameRoomID"></script>

The Partial View "MapSite" 局部视图“ MapSite”

public ActionResult MapSite(int id)
{
   ViewBag.GameRoomID = id;
   return PartialView("MapSite");
}

is rendered into my main page like this: 呈现在我的主页中,如下所示:

<td id="map">
    @{Html.RenderAction("MapSite", "JS");}
</td>

That works perfectly fine! 效果很好! But if I want to render MapSite again at runtime (with new values) like this: $('#map').load('/JS/MapSite/4') (4 is static for testing), the Partial View comes without the DynMap Javascript. 但是,如果我想像这样在运行时再次渲染MapSite(具有新值): $('#map').load('/JS/MapSite/4') (4用于测试是静态的),则部分视图不包含DynMap Javascript。

Is this a bug? 这是错误吗? Isn't it possible to load external Javascript this way "live"? 不可能以这种方式“实时”加载外部Javascript吗? It even hits the breakpoint inside the Controller DynMap method, but the map is empty, because the DynMap values are missing. 它甚至命中Controller DynMap方法内的断点,但由于缺少DynMap值,因此映射为空。

Solution

I found out that my approach is working perfectly fine! 我发现我的方法工作得很好! The problem was in MY specific case that the Javascript content was loaded after the html page itself. 问题是在我特定的情况下,JavaScript内容是 html页面本身之后加载的。 So the window.onload() inside the Javascript couldn't be executed and I just had to do it manually afterwards. 因此,无法执行Javascript中的window.onload(),之后我只需要手动执行即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM