Running Javascript after partial MVC Ajax..BeginForm postback

I have the following script using ASP.NET MVC2

I have a partial view that uses Ajax.BeginForm to allow partial postback, with the OnSuccess AjaxOptions property pointing to the js function onPartialReloadSuccess. UpdateTargetId is hosted on the parent view of the Application, as specified in the BeginForm script.

This partial view itself displays a number of other partial views whose names are dynamically generated 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 this: after partial postback, the function of the onPartialReloadSuccess jS function is successfully called without problems, but the javascript contained in the subviews is not updated.

At first they were configured to run after jQuery $ (document) .ready () ... which obviously will not work on partial postback

My question is, is there a way to ensure that javascript is run on these reprocessed partial views? I see many solutions for asp.net forms (using scriptmanager and PageRequestManager), but nothing special for ASP.NET MVC?

in advance for any help

+4
source share
2 answers

You can separate these scripts into separate functions, and then explicitly call this function in the onPartialReloadSuccess function. Also name them in document.ready so that bootstrapping also works.

0
source

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

I worked on ASP.NET MVC2 for a while, and I had a problem when MVC2> Client Validation did not work, when I dynamically loaded partial view through MVC2> AJAX. After further investigation, I found that the problem was not limited to MVC2> Client Validation, but to all JavaScript that dynamically loads via MVC2 AJAX. Problem> is related to how the response is entered into the DOM element through the> InnerHTML property. Any script block entered in this property will not be executed.

0
source

All Articles