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 8227909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:17:29+00:00 2026-06-07T16:17:29+00:00

I currently have a JSON object on my page which gets built up as

  • 0

I currently have a JSON object on my page which gets built up as users add items to it. This is all done in JavaScript/jQuery.

When the user is done adding items, I want to POST this object to a controller action and have the action return a strongly typed view using this data.

Currently, I have the jQuery.ajax POST sending this JSON object to an Action Method which then binds this object to my strongly typed Model. Problem is, I actually want this jQuery.ajax POST to redirect as if the JSON object were in a FORM and simply being submitted.

I also can’t use the jQuery.post() method, which would redirect as required, as I need to be able to set the contentType to “application/json” so my binding works correctly. Unfortunately, the jQuery.post() method doesn’t allow you to set this parameter.

I’ve read that the jQuery.post() method basically uses the jQuery.ajax() method, so I’ve been battling to get the jQuery.ajax() method to redirect.

I’ve also read that I can set the default contentType for all jQuery.ajax() methods which would then allow me to use the jQuery.post() method but want to try avoid this if possible.

Thanks

Edit: Updated with Saedeas suggestion:

My JavaScript on the ‘Index’ View:

<script language="javascript" type="text/javascript">

    //  Initialize the Shopping Cart object
    var m_ShoppingCart = {
        UserId: 10,
        DeliveryInstructions: "Leave at front desk",
        CartItems: []
    };

    $(document).ready(function () {
        $.extend({
            postJSON: function (url, data, callback) {
                return $.ajax({
                    type: "POST",
                    url: url,
                    data: JSON.stringify(data),
                    success: callback,
                    dataType: "json",
                    contentType: "application/json",
                    processData: false
                });
            }
        });
    });

    function PostDataWithRedirect() {
        var url = '@Url.Action("ConfirmOrder", "Store")';
                $.postJSON(url, m_ShoppingCart, function () { });
    }

    function AddToCart(id, itemName, price, quantity) {

        //  Add the item to the shopping cart object
        m_ShoppingCart.CartItems.push({
            "Id": id,
            "ItemName": itemName,
            "Price": price.toFixed(2), //   Issue here if you don't specify the decimal     place
            "Quantity": quantity
        });

        //  Render the shopping cart object
        RenderShoppingCart();
    }



    function RenderShoppingCart() {

        $("#CartItemList").html("");

        var totalAmount = 0;

        $.each(m_ShoppingCart.CartItems, function (index, cartItem) {

            var itemTotal = Number(cartItem.Price) * Number(cartItem.Quantity);
            totalAmount += itemTotal;

            $("#CartItemList").append("<li>" + cartItem.ItemName + " - $" +     itemTotal.toFixed(2) + "</li>");
        });

        $("#TotalAmount").html("$" + totalAmount.toFixed(2));
    }
</script>

And then the Controller Action ‘ConfirmOrder’

[HttpPost]
public ActionResult ConfirmOrder(ShoppingCartModel model)
{
    return View(model);
}

So when the PostDataWithRedirect() JavaScript method is called it must hit the ConfirmOrder Controller Action and be redirected to the ConfirmOrder View. The Shopping Cart object on my Index view is built up entirely in JavaScript and the user then clicks a ‘Proceed to Checkout’ button and is redirected etc.

PS: My full working example can be found in an the article [ How to POST a JSON object in MVC ], I just need to update this code so that it can do the post and redirect as explained above

  • 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-06-07T16:17:31+00:00Added an answer on June 7, 2026 at 4:17 pm

    Return a view that redirects:

    controller

    return View("redirectionView");
    

    view

    RedirectionView.cshtml
    @{
    Layout = null;
    }
    
    <script type="text/javascript">
      alert("Success! Redirecting...");
      window.location = "./";
    </script>
    

    EDIT

    To accommodate data retention use tempdata.

    controller

    TempData["collectedUserData"] = collectedData;
    return View("redirectionView");
    

    RedirectionView.cshtml

    @{
    Layout = null;
    }
    
    <script type="text/javascript">
     alert("Success! Redirecting...");
     window.location = "./Rebuilder/ActionMethod";
    </script>
    

    Controller Rebuilder

    public ActionResult ActionMethod()
    {
     if( TempData.ContainsKey("collectedUserData") )
     {
      var collectedData = TempData["collectedUserData"];
     }
     //todo: use else clause to catch data not present
             use collectedData to build new view
     return View();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently I have the following which parses a json api.. import simplejson import urllib2
Supposing I have the following code which returns a Javascript object which I can
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
I have an app that received JSON data from the server. Currently when I
I currently have a table of concentrations, which are linked to a table of
Currently I have a view which I render to a template and return it
Consider this Django view which will get a list of items associated to the
I have a JSON feed of articles with 10 articles per page. JSON structure
I have a need for generating JavaScript on the server. I can do this
I have a Page that expects a POST Request and returns some JSON. Essentially

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.