简体   繁体   English

在ASP.NET MVC的部分视图中执行Javascript

[英]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标签内使用此JavaScript代码,以便使用浏览按钮填充div,以便用户可以上传图片(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 因此,如果更新成功,将调用swfu函数

The point of window.onload is to execute the given function once the page has finished... loading. window.onload的要点是在页面完成后执行给定的功能。 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. 部分视图使用不评估JavaScript代码的Microsoft Ajax呈现。 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 我建议在这种情况下,不要使用局部视图,也不要使用Spark等其他视图引擎。或者将所有必需的javascript放在父视图中,这会使您的代码有点草率

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

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