简体   繁体   中英

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 .

I call this Javascript in my Partial View "MapSite" with

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

The Partial View "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.

Is this a bug? Isn't it possible to load external Javascript this way "live"? It even hits the breakpoint inside the Controller DynMap method, but the map is empty, because the DynMap values are missing.

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. So the window.onload() inside the Javascript couldn't be executed and I just had to do it manually afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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