简体   繁体   中英

asp.net MVC Partial Views how to initialise javascript

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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