Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6884175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:28:29+00:00 2026-05-27T05:28:29+00:00

In an ASP.NET MVC app I use jQuery for posting data on button-click: <button

  • 0

In an ASP.NET MVC app I use jQuery for posting data on button-click:

<button onclick="addProducts()">Add products</button>
....
$.post('<%= Url.Action("AddToCart", "Cart") %>',
            {
                ...
                returnUrl: window.location.href
            });

In the “AddToCart” action of “Cart” controller I use redirection to another View after posting:

    public RedirectToRouteResult AddToCart(..., string returnUrl)
    {
        ...
        return RedirectToAction("Index", new { returnUrl });            
    }

All is okay, except this redirection. I stay on the same page after posting. I suspect it’s due to AJAX type of “POST” request.

How to solve the problem with jQuery POST request blocking the redirection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:28:29+00:00Added an answer on May 27, 2026 at 5:28 am

    I created a $.form(url[, data[, method = 'POST']]) function which creates a hidden form, populates it with the specified data and attaches it to the <body>. Here are some examples:

    $.form('/index')
    
    <form action="/index" method="POST"></form>
    
    $.form('/new', { title: 'Hello World', body: 'Foo Bar' })
    
    <form action="/index" method="POST">
        <input type="hidden" name="title" value="Hello World" />
        <input type="hidden" name="body" value="Foo Bar" />
    </form>
    
    $.form('/info', { userIds: [1, 2, 3, 4] }, 'GET')
    
    <form action="/info" method="GET">
        <input type="hidden" name="userIds[]" value="1" />
        <input type="hidden" name="userIds[]" value="2" />
        <input type="hidden" name="userIds[]" value="3" />
        <input type="hidden" name="userIds[]" value="4" />
    </form>
    
    $.form('/profile', { sender: { first: 'John', last: 'Smith', postIds: null },
                         receiver: { first: 'Foo', last: 'Bar', postIds: [1, 2] } })
    
    <form action="/profile" method="POST">
        <input type="hidden" name="sender[first]" value="John">
        <input type="hidden" name="sender[last]" value="Smith">
        <input type="hidden" name="receiver[first]" value="John">
        <input type="hidden" name="receiver[last]" value="Smith">
        <input type="hidden" name="receiver[postIds][]" value="1">
        <input type="hidden" name="receiver[postIds][]" value="2">
    </form>
    

    With jQuery’s .submit() method you can create and submit a form with a simple expression:

    $.form('http://stackoverflow.com/search', { q: '[ajax]' }, 'GET').submit();
    

    Here’s the function definition:

    jQuery(function($) { $.extend({
        form: function(url, data, method) {
            if (method == null) method = 'POST';
            if (data == null) data = {};
    
            var form = $('<form>').attr({
                method: method,
                action: url
             }).css({
                display: 'none'
             });
    
            var addData = function(name, data) {
                if ($.isArray(data)) {
                    for (var i = 0; i < data.length; i++) {
                        var value = data[i];
                        addData(name + '[]', value);
                    }
                } else if (typeof data === 'object') {
                    for (var key in data) {
                        if (data.hasOwnProperty(key)) {
                            addData(name + '[' + key + ']', data[key]);
                        }
                    }
                } else if (data != null) {
                    form.append($('<input>').attr({
                      type: 'hidden',
                      name: String(name),
                      value: String(data)
                    }));
                }
            };
    
            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    addData(key, data[key]);
                }
            }
    
            return form.appendTo('body');
        }
    }); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add simple login page to ASP.NET MVC app. I actually use
I use WCF with my ASP.NET MVC app, my data service get data from
In my Controller in a Asp.net MVC 1 app I want to use UpdateModel
I have an ASP.NET MVC app and I want to add to each page
I'm working on a new ASP.NET MVC / JQuery app which will need to
We've an ASP.Net MVC2 web app (SQL 2008 in backend). We use Data Annotations
I have an ASP.NET MVC App, which use EF code First, for some reason
I'm planning a new service for my ASP.NET MVC app and want to use
I am developing an ASP.NET MVC app and I've been looking into using Data
I am trying to use the JQuery plugin jqGrid with an asp .net mvc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.