开发者

asp.net MVC Partial Views how to initialise javascript

开发者 https://www.devze.com 2023-01-02 08:25 出处:网络
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

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消