Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Javascript after an MVC Ajax..BeginForm partial postback

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

like image 981
youthinkthisisntme Avatar asked Nov 14 '22 16:11

youthinkthisisntme


1 Answers

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.

like image 56
Darin Dimitrov Avatar answered Nov 17 '22 00:11

Darin Dimitrov