简体   繁体   中英

Execute Javascript inside a partial view in ASP.NET MVC

I use this JavaScript code inside the head tag in order to populate the divs with a browse button so that users can upload images (swfupload).

<head>
...
<script type="text/javascript">
    var swfu = function() {
        return new SWFUpload({
            // Backend Settings
            // settings go here...
            // Too long to display here
            // Debug Settings
            debug: false
        });
    }
    window.onload = swfu;
</script>
</head>

....

<div id="swfu_container" style="margin: 0px 10px;">
    <div>
    <span id="spanButtonPlaceholder"></span>
</div>
<div id="divFileProgressContainer" style="height: 75px;"></div>
<div id="thumbnails"></div>
</div>

This works well, but the problem is when I try to put this code inside of a partial view. So far, I haven't be able to get this to work.

Is there anyone more experienced to the rescue?

Thank you

You can put your function like this:

<%= Ajax.ActionLink("YourAction",new AjaxOptions{ OnSuccess="swfu", UpdateTargetId = "spanButtonPlaceholder" }) %> 

so your swfu function will be called if your update is successfull

The point of window.onload is to execute the given function once the page has finished... loading. That said, you should consider moving the onload to the bottom of the body. And I'm not the worlds biggest fan of keeping scripts in the head. :)

Partial views are rendered with Microsoft Ajax which does not evaluate JavaScript code. I would suggest looking at not using partial views in this case or look at other view engines like Spark....or put all required javascript in your parent view which kinda makes your code a bit sloppy

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