views:

572

answers:

1

I have an Asp.net MVC partial view that is used for searching. It does an Ajax call to retrieve results. 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. 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. 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?

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?

+1  A: 

I ended up using a sort of TemplateMethod pattern to make this work. In my partial I have added the JavaScript function:

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. I am not sure how to check the parameters match, but at least I get a call when the partial has finished its work.

SteveBering