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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:11:38+00:00 2026-05-23T00:11:38+00:00

I have the following action method, when I press the update button on my

  • 0

I have the following action method, when I press the update button on my cart and post to this method I need it bind all productId and partquantity values into the respective parameters/arrays (int[] ProductId, int[] partquantity) and it does this. I am presuming when form data, that is keys and values are posted they arrive in some sort of order, likely as elements are laid out on the HTML page (top to bottom)? So I wish for the operation on each cart item to be performed using the correct partquantity entered, that is for the correct productId. I am guessing if they post and bind in strict order then partquantity[2] should be the correct quantity for ProductId[2] etc. ?

The below logic in trying to increment f by 1 for each operation on each productId in the ProductId[] array does not work. I need to get this to work because say I have 5 items added to the cart and change the quantity for 4 of them I wish to just press the one update button and it will update for all these items\lines in the cart. So method needs to catch all the posted productId and quantities and use in the correct order, so the right quantity is assigned to the right cart item which is looked up by ProductId.

public RedirectToRouteResult UpdateCart(Cart cart, int[] ProductId, int[] partquantity, string returnUrl)
{
   int f = 0;
   int x = partquantity.Length;

   while (f <= x) 
   {
     foreach (var pid in ProductId)
     {
       f++;
       var cartItem = cart.Lines.FirstOrDefault(c => c.Product.ProductID == pid);
       cartItem.Quantity = partquantity[f];
     }
   }
   return RedirectToAction("Index", new { returnUrl });
 }

This is the View:

<% foreach (var line in Model.Cart.Lines)
 { %>
  <tr>
    <td align="center"><%: Html.TextBox("partquantity", line.Quantity)%></td>
    <td align="left"><%: line.Product.Name%></td>
    <td align="right"><%: line.Product.ListPrice.ToString("c")%></td>
    <td align="right">
    <%: (line.Quantity * line.Product.ListPrice).ToString("c")%>
    </td>
  </tr>
<%: Html.Hidden("ProductId", line.Product.ProductID)%>
<% } %>

Custom Binder

    public class CartModelBinder : IModelBinder
{

    private const string cartSessionKey = "_cart";

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {

        if (bindingContext.Model != null)
            throw new InvalidOperationException("Cannot update instances");

        Cart cart = (Cart)controllerContext.HttpContext.Session[cartSessionKey];

        if (cart == null)
        {
            cart = new Cart();
            controllerContext.HttpContext.Session[cartSessionKey] = cart;

        }

        return cart;

    }
}

}

  • 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-23T00:11:38+00:00Added an answer on May 23, 2026 at 12:11 am

    Your Cart object should be enough as parameter for this scenario, you can have something like this.

    The general idea would be to use an index so you get your Cart object with your Lines as you passed them to the view originally but with updated Quantity values.

    Your model as I understand it:

    public class Cart
    {
      ...
      public List<CartItem> Lines {get; set; }
    }
    
    public class CartItem
    {
      public Product Product {get; set;}
      public int Quantity {get; set;}
      ...
    }
    

    In your view:

    @model Cart
    ...
    @using(Html.BeginForm())
    {
      @{ int index = 0; }
      @foreach(var l in Model.Lines)
      {
        @Html.Hidden("cart.Lines.Index", index);
        @Html.Hidden("cart.Lines[" + index + "].Product.ProductID", l.Product.ProductID)
        @Html.TextBox("cart.Lines[" + index + "].Quantity")
        @{ index++; }
      }
      <input type="submit" value="Update Quantity" />
    }
    

    Your controller:

    public ActionResult UpdateCart(Cart cart)
    {
      // you should have new values on Quantity properties of the cart.Lines items.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: <form action= method=POST> <?php $count = isset($_POST['count']) ? $_POST['count']
I have the following form <form id=myform method=post action=#> <div class=fields> <fieldset class=ui-widget ui-widget-content
I have the following Action Method that I'm trying to redirect from if the
I have the following Action method. public ActionResult Action() { var model = new
I have the following Action Method: [HandleFtmsError] public ActionResult PerformanceChart(ChartViewModel chart) { var x
I have a button on my windows form that calls the RunWorkerAsync() method, this
In an action method, I have the following excerpt of code: error_reporting(E_ALL); ini_set('display_errors', '1');
I have an action method in my UpdatesController with the following signature: public JsonResult
Supposing I have the following action: def index @posts = Post.joins(:tags).where(:tags => {:id =>
I have the following controller action and test. I'm new to testing with Shoulda

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.