5
votes

I've got the following scenario using ASP.NET MVC2

I have a partial view that uses an Ajax.BeginForm to allow a partial postback, with the OnSuccess property of the AjaxOptions pointing to the js function onPartialReloadSuccess. The updateTargetId is housed on a parent View "Application" as referenced in the BeginForm script.

This partial view itself renders a number of other partial views, the names of which are generated dynamically based on the model

<% using (Ajax.BeginForm("Application", new AjaxOptions { UpdateTargetId = "mainframe", OnSuccess = "onPartialReloadSuccess" })) { %>

    <div id="breadcrumbs">
        <% Html.RenderPartial(Model.Breadcrumbs, Model);%>
    </div>

    <div id="action">
        <% Html.RenderPartial(Model.CurrentView, Model);%>
    </div>

<% } %>

My problem is as follows - after a partial postback the 'onPartialReloadSuccess' js function is successfully called without problem, but non of the javascript contained within the sub-views is re-run.

They were initially set up to run after a jQuery $(document).ready()... which obviously won't work on a partial postback

My question is this - is there any way to ensure that the javascript on these re-rendered partial views is run? I see a lot of solutions for asp.net forms (using scriptmanager and PageRequestManager), but nothing specific for ASP.NET MVC?

thanks in advance for any help

2
Doesn't work OOTB in ASP.NET MVC 3 since it uses jQuery AJAX (which magically runs JS in loaded content)? Then perhaps using jQuery AJAX could be an option for you as well.bzlm

2 Answers

1
votes

You could externalize those scripts into separate function(s) and then in the onPartialReloadSuccess function explicitly call this function(s). Also call them in the document.ready so that the initial load also works.

0
votes

looks like I found the answer I required at http://adammcraventech.wordpress.com/2010/06/11/asp-net-mvc2-ajax-executing-dynamically-loaded-javascript/ with the following quote explaining the problem

I have been tinkering with ASP.NET MVC2 for a while and I had the problem where the MVC2 >Client Validation did not work when I was dynamically loading a partial view through MVC2 >AJAX. Upon further investigation, I discovered that the issue was not limited just to MVC2 >Client Validation, but to all JavaScript that is dynamically loaded through MVC2 AJAX. The >issue has to do with the way that the response is injected into the DOM element – through the >InnerHTML property. Any script block injected into that property will not be executed.