What is the best way to execute JavaScript on the main MVC page when an AJAX request in a partial view has finished?

My Asp.net MVC partial view is designed for searching and makes an Ajax call to retrieve results. After the results are displayed, the user can select a search result by clicking on a link in one of the rows. Upon selecting a search result, an Ajax post request updates some state. Following this Ajax call completion, I aim to redirect to another page depending on the current page the user is on. Since this search partial view will be used on multiple pages with different redirect locations, I am struggling to find a solution that doesn't overly tie the partial view and hosting page together.

Another option might involve redirecting to another page after the selection Ajax call finishes, but wouldn't a redirect result within an Ajax call be ignored?

Alternatively, is there a way to include a button or link on each row that triggers a post request leading to a redirect result, based on the calling page?

Answer №1

After some experimentation, I decided to utilize a form of TemplateMethod design pattern to achieve the desired outcome. Within my partial, I included the following JavaScript function:

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

This function checks for the existence and type of the specified function, calling it if present. By implementing the method `DoSomethingInParentPage` in the hosting page, additional actions can be taken upon completion of an action within the partial. While I have yet to figure out how to validate parameter matching, I am satisfied with receiving a notification once the partial completes its task.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on avoiding redirection when submitting a form

Upon making an AJAX call to a page, I receive a form with user parameters. This form is later submitted to a URL in order to create a session for the same user in advance. When that person visits the site, they should see their name displayed. To achieve ...

There seems to be a problem with the sorting functionality on the table in React JS,

My React table is functioning well with all columns except for the country name column. I double-checked the API and everything seems to be in order, but I'm stuck on how to troubleshoot this issue. const Table = () => { const[country, setCount ...

Unable to get jQuery waypoints to function on local server

Currently, I am working with jQuery. I downloaded an example shortcut for infinite scroll from jQuery Waypoints and tried to use it. However, when the page reaches the end, the web console displays the following error: XMLHttpRequest cannot load file:// ...

When I attempted to run `npm start`, an error with status code 1 was thrown,

Upon running npm start, the following error is displayed: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d4c5d5d6d1d031c031d">[email protected]</a> start /Users/user/Desktop/react-tutorial > react-script ...

What is the best way to transfer information between Ajax and a RestController in a Spring Boot application?

I am looking to utilize Ajax to send data to a RestController using the POST method for processing, and then return the resulting list back to Ajax. Controller @Controller public class AjaxController { @GetMapping("/web") public String web() ...

Retrieving image URL through a jQuery AJAX request

I'm trying to set a src attribute for a dynamically created element. The src needs to be a URL fetched from the Giphy API. I can successfully log the response data, but I'm struggling with getting the jQuery code right for creating a container to ...

Error: Reactjs - Attempting to access the 'name' property of an undefined variable

As I continue to learn about React, I have been experimenting with props in my code. However, I encountered an error where the prop is appearing as undefined. This issue has left me puzzled since I am still at a basic level of understanding React. If anyo ...

Best practices for executing an asynchronous forEachOf function within a waterfall function

I've been working with the async library in express js and I'm encountering an issue when using two of the methods alongside callbacks. The variable result3 prints perfectly at the end of the waterfall within its scope. However, when attempting t ...

The Rails AJAX redirect is directing to the incorrect controller action

My goal is to implement an AJAX redirect to my custom 'on_clan_change' controller action. I am initiating the AJAX call with the following function: $(document).on('change', '#character_clan_id', () => { $.ajax({ url ...

Console Error: Attempting to set the 'className' property of null object results in an

I'm experiencing difficulties setting up the code. The function should display all songs by a specific artist when their name is entered and the button is pressed. JavaScript file: /** * Utilizes AJAX to request data about artists from an online sou ...

At times, the Ajax response may be lacking in content. However, the response tab in Google

My goal is to retrieve responses from an AJAX call and store them in the global scope for easy access throughout my website. var getOrderStatus = getOrderStatus(), getUserData = getUserData(), orderFormReady = $.when(getOrderStatus, getUserData), ...

Matching numbers that begin with zero or are completely optional using Regex

Attempting to come up with a regex pattern that will allow the entry of the specified input into an HTML input field: The input must begin with 0 The input can be left empty and characters may be deleted by the user ^[^1-9]{0,1}[0-9\\s-\& ...

Steps for adjusting the length in the getRangeLabel function of mat paginator

@Injectable() export class MyCustomPaginatorIntl extends MatPaginatorIntl { public getRangeLabel = (page: number, pageSize: number, length: number): string => { if (length === 0 || pageSize === 0) { return `${ ...

changing the validation function from using if statements to utilizing switch statements

Currently, I am refactoring a form in react and planning to offload most of the state and logic to the parent component. This decision is made because the parent's state will be updated based on the form submission results. However, I encountered an i ...

JavaScript game with server-side communication and answer validation functionality

In my fast-paced, quiz-like Javascript game, users must answer a series of Yes/No questions as quickly as possible. Upon answering, the response is sent to the server for validation and feedback (correct/incorrect) before moving on to the next question usi ...

Vue.js is like the modern version of jquery-cron

Is there a similar tool to jquery-cron with an easy-to-use text/select interface that anyone knows of? I've come across vue-cron and point-vue-cron, but they seem too complicated for my needs. ...

Using Vue.js to toggle rendering based on checkbox selection

Struggling to conditionally render form elements in Vue based on user input. I can do this with VanillaJS or jQuery, but struggling to implement it with Vue's built-in conditional directives. Using single-file components with the webpack template from ...

Reset filter when the "All" option is chosen in Vue.js

There is an array of objects that looks like this: obj= [{name: 'joe', job: 'teacher', city: 'miami'}, {name: 'bill', job: 'police', city: 'yorkshire'}, {name: 'sarah', job: 'driver ...

Retrieve distinct ajax endpoint

I have a piece of code that rotates photos every few seconds using jQuery. Here is my jQuery code: setInterval("rotator();",4000); function rotator(){ var i = 0; var zdjecie = ''; $('#rotate').html(&apo ...

Reactivity in Vue 3 with globalProperties

When attempting to migrate a Vue 2 plugin to Vue 3, I encountered an issue in the install function. In Vue 2, I had code that looked like this: install(Vue, options) { const reactiveObj = { // ... }; Vue.prototype.$obj = Vue.observable(reactiveO ...