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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:50:02+00:00 2026-06-01T13:50:02+00:00

Here is the javascript code I use to create the array and send it

  • 0

Here is the javascript code I use to create the array and send it on it’s way:

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $("#update-cart-btn").click(function() {
            var items = [];
            $(".item").each(function () {
                var productKey = $(this).find("input[name='item.ProductId']").val();
                var productQuantity = $(this).find("input[type='text']").val();
                items[productKey] = productQuantity;
            });

            $.ajax({
                type: "POST",
                url: "@Url.Action("UpdateCart", "Cart")",
                data: items,
                success: function () {
                    alert("Successfully updated your cart!");
                }
            });
        });
    });
</script>

The items object is properly constructed with the values I need.

What data type must my object be on the backend of my controller?

I tried this but the variable remains null and is not bound.

[Authorize]
[HttpPost]
public ActionResult UpdateCart(object[] items) // items remains null.
{

    // Some magic here.
    return RedirectToAction("Index");
}
  • 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-01T13:50:04+00:00Added an answer on June 1, 2026 at 1:50 pm

    If you are going to send JSON to the server you need to JSON.stringify the data and specify the contentType as application/json to play nice with the MVC3 model binder:

        $.ajax({
                type: "POST",
                url: "@Url.Action("UpdateCart", "Cart")",
                data: JSON.stringify(items),
                success: function () {
                    alert("Successfully updated your cart!");
                },
                contentType: 'application/json'
            });
    

    And as the datatype on the server you can use strongly typed classes eg:

    public class Product
    {
        public int ProductKey { get; set; }
        public int ProductQuantity { get; set; }
    }
    
    [HttpPost]
    public ActionResult UpdateCart(Product[] items)
    {
    
        // Some magic here.
        return RedirectToAction("Index");
    }
    

    But you need to tweak the items list a bit:

    var items = [];
    $(".item").each(function () {
       var productKey = $(this).find("input[name='item.ProductId']").val();
       var productQuantity = $(this).find("input[type='text']").val();
       items.push({ "ProductKey": productKey, "ProductQuantity": productQuantity });
    });
    

    Basically the JSON object structure should match the C# model class structure (also the property name should match) and then the model binder in MVC takes care to populate your server side models with the JSON data that you’ve sent. You can read more about model binders here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my code.. <script type=text/javascript> function clicker(){ var thediv=document.getElementById('downloadoverlay'); if(thediv.style.display == none){ thediv.style.display
Here's my code <script type=text/javascript> function getNextScroll(i){ <asp:Literal ID=infiniteScrollTableJS runat=server></asp:Literal> } </script> I am
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
I use BBC carousel http://www.bbc.co.uk/glow/docs/1.7/furtherinfo/widgets/carousel/ . Here is my script code <script src=/themes/javascript/glow/1.7.7/core/core.js type=text/javascript></script>
I have the following code in javascript: <script type=text/javascript src=#{facesContext.externalContext.requestContextPath}/js/sample-points.js></script> <script type=text/javascript>//<![CDATA[ var cloudmade
I create a secondary browser window with Javascript code, using the window.open function, and
Beginners Javascript question here. I am trying to create a function that finds all
Here is my problem, I need to create a javascript function that takes 2
Here is the object and global namespace for my Javascript code. Users.Cache.data = {
I need to make javascript code to swap images with option change. Here is

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.