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

发布于 2024-09-03 21:17:46 字数 247 浏览 2 评论 0原文

我有一个编辑表单,它使用 ajax 表单提交到控制器。根据提交的数据,我将用户重定向到两个页面之一(通过返回部分视图)。这两个页面都依赖于 javascript/jquery,并且都不使用页面之间的任何通用内容。

在每个页面上初始化这些 javascript 的最佳方法是什么?我知道有 AjaxOption OnComplete 但两个页面都非常动态,具体取决于传递的模型,我宁愿将两个页面的 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2024-09-10 21:17:46

如果您使用的是 jQuery,那么在部分页面中您可以编写

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

Update: 以上将不起作用。所以你可以试试这个。

您可以在 AjaxOption.OnComplete 上调用 On_PartialLoad() 方法。每个部分都可以提供自己的 On_PartialLoad() 实现。

所以在 Partial1 中你可以拥有

function On_PartialLoad(){
  //Partial_1 Implementation

}

所以在 Partial2 中你可以拥有

function On_PartialLoad(){
  //Partial_2 Implementation

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文