views:

107

answers:

1

Hi,

I have an edit form that uses an ajax form to submit to the controller. Depending on the data submitted I redirect the user to one of two pages (by returning a partial view). Both pages rely on javascript/jquery and neither use anything common between the pages.

What is the best way to initialise these javascripts on each page? I know there is the AjaxOption OnComplete but both pages are quite dynamic depending on the Model passed and I would rather keep the javascript for both pages seperate rather than having a common method.

Thanks

A: 

If you are using jQuery then in the partial pages you can write

$(document).ready(function() {
   // put all your javascript initialisation here.
 });

Update: Above will not work. So you can try this.

You can call a method on On_PartialLoad() on AjaxOption.OnComplete. Each of this partial can provide there own implementation of On_PartialLoad().

So In Partial1 you can have

function On_PartialLoad(){
  //Partial_1 Implementation

}

So In Partial2 you can have

function On_PartialLoad(){
  //Partial_2 Implementation

}
Amitabh
This won't work as the page has already loaded and $(document).ready does not fire on partial page updates.
Simon G
$(document).ready will fire.http://stackoverflow.com/questions/312718/jquery-document-ready-and-asp-net-mvc-partialBut you need to have the $(document).ready in the partial not in the containing page
Malcolm Frexner
@Simon: Let us also know which one worked.
Amitabh
I used the common method name and put the JavaScript in the partial
Simon G