简体   繁体   English

asp.net MVC部分视图如何初始化javascript

[英]asp.net MVC Partial Views how to initialise javascript

I have an edit form that uses an ajax form to submit to the controller. 我有一个使用ajax表单提交给控制器的编辑表单。 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. 两个页面都依赖javascript / jquery,并且两个页面之间都不使用任何通用的东西。

What is the best way to initialise these javascripts on each page? 在每个页面上初始化这些JavaScript的最佳方法是什么? 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. 我知道这里有AjaxOption OnComplete,但是两个页面都是动态的,具体取决于传递的模型,我宁愿将两个页面的javascript分开,而不是使用通用方法。

Thanks 谢谢

If you are using jQuery then in the partial pages you can write 如果您使用的是jQuery,则可以在部分页面中编写

$(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. 您可以在AjaxOption.OnComplete的On_PartialLoad()上调用一个方法。 Each of this partial can provide there own implementation of On_PartialLoad(). 每个部分都可以在那里提供自己的On_PartialLoad()实现。

So In Partial1 you can have 因此,在Partial1中,您可以拥有

function On_PartialLoad(){
  //Partial_1 Implementation

}

So In Partial2 you can have 所以在Partial2中,您可以拥有

function On_PartialLoad(){
  //Partial_2 Implementation

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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