简体   繁体   English

部分视图中的Ajax调用完成后,如何在主机MVC页面上调用javascript?

[英]How do I call javascript on host MVC page when ajax call in partial view is complete?

I have an Asp.net MVC partial view that is used for searching. 我有一个用于搜索的Asp.net MVC局部视图。 It does an Ajax call to retrieve results. 它执行Ajax调用以检索结果。 Once the results are retrieved and displayed in the result pane, the user can click on a link in any of the result rows to select one of the search results. 一旦检索到结果并将其显示在结果窗格中,用户就可以单击任何结果行中的链接以选择一个搜索结果。 When the user clicks on a link to select one of the search results, it will make an Ajax post request to update some state. 当用户单击链接以选择搜索结果之一时,它将发出Ajax发布请求以更新某些状态。 Once this Ajax call is complete, I need to redirect to another page, but the destination page depends on which page the user is on. 完成此Ajax调用后,我需要重定向到另一个页面,但是目标页面取决于用户所在的页面。 This search partial view will be hosted in multiple pages and the redirect location will be different for each of the host pages. 此搜索局部视图将托管在多个页面中,并且每个主机页面的重定向位置都将不同。 I cannot think of a way to do this that makes sense and doesn't couple the partial view to the hosting page (or vice versa) excessively. 我想不出一种可行的方法,并且不会将部分视图过多地耦合到托管页面(反之亦然)。

I guess the other option might be to redirect to another page once the selection Ajax call is complete (this is ultimately the goal), but won't a redirect result within an Ajax call get swallowed? 我猜另一个选择可能是选择Ajax调用完成后重定向到另一个页面(这最终是目标),但是Ajax调用中的重定向结果不会被吞下吗?

Or is there a way to have a button or link on each row instead that causes a post request to occur that could return a redirect result, with the destination based on the calling page? 还是有一种方法在每行上都有一个按钮或链接,而不是导致发布请求的出现,该请求可能返回重定向结果,而目标基于调用页面?

I ended up using a sort of TemplateMethod pattern to make this work. 我最终使用了一种TemplateMethod模式来完成这项工作。 In my partial I have added the JavaScript function: 在我的部分文章中,我添加了JavaScript函数:

function onActionCompleted() {
    DoSomethingLocal();
    if (typeof DoSomethingInParentPage == 'function') {
        DoSomethingInParentPage();
    }
} 

This checks to see if the function exists and is a function, and if it is it calls the function. 这将检查该函数是否存在并且是一个函数,如果存在则调用该函数。 In order for this to work, the hosting page can choose to implement the method DoSomethingInParentPage to complete some action when the action is completed in the partial. 为了使此工作有效,托管页面可以选择实现DoSomethingInParentPage方法,以在部分完成操作时完成某些操作。 I am not sure how to check the parameters match, but at least I get a call when the partial has finished its work. 我不确定如何检查参数是否匹配,但是至少我在部分完成工作后接到了电话。

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

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