Forward after asynchronous JavaScript and XML (AJAX)

Currently, I am working on an MVC project where I need to achieve the following:

The scenario involves sending an ajax request from a JS script and then redirecting to a page along with a model once the request is processed.

I attempted to send a form as the ajax response and submit it using the following method:

sb.Append("<html>");
sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>");
sb.AppendFormat("<form name='form' action='{0}' method='post'>", "url..");
sb.AppendFormat("<input type='hidden' name='result' value='{0}'>", val1);
.....

In the JavaScript section:

success: function (result) {
            var form = $(result);
            $(form).submit();
        }

However, this approach requires specifying each post parameter individually, whereas my goal is to send the entire model object. How can I achieve that?

Edit The complete process is outlined below:

1. Development is being carried out in an MVC application.

2. A submit button in the view triggers redirection to the JavaScript code.

3. The JavaScript code initiates an Ajax request to a specific MVC page named 'Transaction'.

4. Once certain actions are performed in the C# code, I need to redirect the user to a page called EOF with a large number of post parameters, which is why I wish to pass it as a ViewModel.

Answer №1

Utilizing ajax, you have the option to send back the URL from your action and then redirect using javascript:

Control Panel

public ActionResult Transaction()
{
    // Perform actions
    return Json(new { success = true, redirecturl = Url.Action("RedirectedAction") });
}

You can then include something similar to this in your success function within your javascript code:

Javascript (within your success handler)

success: function (result) {
            if (result.success == true)
            {
               window.location = result.redirecturl;
            }
        }

Answer №2

Here is a possible solution:

public IActionResult CustomFunction(string parameter)
{
   // Implement your logic here
    return View("EndOfFunction");
   // Alternatively, if you want to pass a model to EndOfFunction:
   // return View("EndOfFunction", myModel);
}

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

What enables the SmtpClient to operate efficiently even in the absence of a host name?

My ASP.NET application is currently running on a Windows Server 2003 within a local area network, specifically inside a state government firewall. One of its main functions involves sending emails to users for various reasons such as registrations and pass ...

What is the reason for the new page rendering from the bottom of the screen in React?

Whenever I navigate between pages in my React project, the page always starts at the bottom instead of staying at the top after rendering. I am using router v5 and have been unable to find a solution specifically for this version. I have attempted differe ...

How come my effort to evade quotation marks in JSON isn't successful?

When trying to parse a JSON-string using the JQuery.parseJSON function, I encountered an error message that read: Uncaught SyntaxError: Unexpected token R. This was unusual as the only uppercase 'R' in my JSON-formatted string appeared right afte ...

Creating a digital marketplace for "New Arrivals"

I've been grappling with creating a 'Latest Products' page for my eShop project. My goal is to showcase up to 10 products from a database table that have been added in the last 30 days or less. Initially, I attempted to utilize the GridView ...

Utilizing event delegation for JavaScript addEventListener across multiple elements

How can I use event delegation with addEventListener on multiple elements? I want to trigger a click event on .node, including dynamically added elements. The code I have tried so far gives different results when clicking on .title, .image, or .subtitle. ...

I am attempting to save the input value to an array in local storage using VUEX. However, my current method is not producing the desired results

I am facing an issue where I am trying to push an input value and store it in an array in the local storage using VUEX. I have created an array in the state for this purpose. Initially, I push the value and then I stringify it to set the value to the sto ...

The issue with the vue-carousel arises from an uncaught TypeError in which it is unable to read properties of undefined while trying to access '_c'. This problem occurs within the vue-carousel.min.js file

I encountered an error when using the Carousel component from the plugin. The error message is as follows: vue-carousel.min.js:6 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_c') at Proxy.r (vue-carousel.min.js: ...

Concerns regarding rendering HTML and drawing in Node.js

Having an issue where node.js is serving an html file with an SVG drawing. The index.html features a chart similar to the line chart found at http://bl.ocks.org/mbostock/3883245 Whenever I try to serve the index.html through the express server, I end up ...

How can I prevent my JSON object from serializing .NET nulls as "null" for object members?

I am currently utilizing WebMethods to retrieve an array of a custom class. When this array is returned from a Jquery .ajax call, it gets serialized into a JSON object that can be utilized with Javascript in my ASP.NET application. The issue I am facing is ...

Sending Emails with AngularJS

Exploring AngularJs has been a delightful experience for me, and I recently stumbled upon a fantastic plugin for Angular called angular-http-auth. This plugin allows you to seamlessly integrate an identification system. Does anyone know of a similar resou ...

What is the best way to display the HTML content being received from the database in the DOM?

I have received an object in this format [ { "BET": 57630343, "CUSTOMER": 181645, "SPORT": "MLB", "XX_FILL OPEN": "<button class=\"btn\" onclick=\"fillOpen(57630343)\">Fill Open</button>", "XX_VIEW": n ...

What are the best strategies to guard against JsonConvert Deserialize object errors in cases where an object class property receives unexpected data?

When converting Json data responses into C# classes using http://json2csharp.com, I have encountered a common issue. If certain parts of the Json are missing when generating the classes, it can lead to an incomplete set of class properties, resulting in un ...

Setting up an Express route for updating data

I am in the process of developing a MEVN stack CRUD application (Vue, Node, Express, MongoDB). I am currently working on setting up an Express route for handling updates in my app... postRoutes.post('/update/:id', async(req, res)=> { cons ...

AngularJS module is experiencing issues with loading properly

Can someone please help me understand what the issue is? I am new to AngularJS and may have overlooked something. Below is my simple HTML code: <!DOCTYPE html> <html> <script type="text/javascript" src="angular.js"></script> ...

What is the best way to combine two sections in html/css/bootstrap?

I've been trying to create a simple webpage with a navigation bar and a section below it, but I keep running into an issue where there's unwanted white space between the nav bar and the next section in blue. Is there a way to eliminate this gap a ...

Having an issue with displaying the country name and country code in a table using the Angular7 custom pipe

country code: "ab", "aa", "fr", ... I need to create a custom pipe that will convert a countryCode into a countryName, such as: "ab" → "Abkhazian", "ch" → "Chinese", "fr" ...

Why is the last move of the previous player not displayed in this game of tic tac toe?

Why does the last move of the previous player not show up in this tic-tac-toe game? When it's the final turn, the square remains empty with neither an "O" nor an "X". What could be causing this issue? Here is the code snippet: The HTML code: <div ...

The Static Interface Binding in TypeScript

I have inquired about how to extend the static functionality of existing objects in JavaScript (using TypeScript). In all examples provided here, I am utilizing Object The code below showcases a polyfill definition for ECMAScript's Object.is function ...

What steps are involved in bundling a Node project into a single file?

Recently, I've been using npm to create projects. When it comes to AMD, I find the native node require to be very convenient and effective for my needs. Currently, I rely on grunt-watchify to run my projects by specifying an entry file and an output f ...

Load upcoming and previous slides in advance

Currently, I have implemented a basic slideshow on my website. It functions properly, but I am interested in preloading the previous and next slides to enhance its speed. Is there anyone who can assist me with this request? ...