Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello House,

I am working on MVC 5 projects, I have a shopping cart table in my views, and in the table there is a text field for quantity where users can change quantity on any product. I have a button at the buttom of the page where I want users to click and update their newly entered quantity. below is my code:

HTML
<table class="table">
                        <thead>
                            <tr>
                                <th colspan="2">Product</th>
                                <th>Quantity</th>
                                <th>Unit price</th>
                                <th>Discount</th>
                                <th colspan="2">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                        @foreach (var Item in Model)
                          {                              
                            <tr>
                                <td>                                       
                                   <img src="@Item.FrontViewThumbNail" alt="@Item.ProductName">                                       
                                </td>
                                <td>
                                   @Item.ProductSku.ToUpper()- @Item.ProductName
                                </td>
                                <td>
                                    <input type="number" value="@Item.ProductQuantity" class="form-control" min="1" id="quantity" name="quantity">                                        
                                    <input type="hidden" value="@Item.ProductID" name="hiddens" />                                  
                                </td>
                                <td> ₦@Item.ProductPrice.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)</td>
                                <td> ₦@ViewBag.Discount</td>
                                <td> ₦@Item.ProductTotalPrice.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)</td>
                                <td>
                                    @*<a href="#"></a>*@
                                    <a href="@Url.Action("ViewCart", "Home", new { id = Item.ProductID })" class="btn btn-xs btn-bricky tooltips" data-placement="top" data-original-title="Delete">^__i class="fa fa-trash-o"></a>
                                </td>
                            </tr>

                            }
                        <tfoot>
                            <tr>
                                <th colspan="5">Total</th>
                                <th colspan="2">₦@ViewBag.CartTotal.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)</th>
                            </tr>
                        </tfoot>


                    </table>

I want to be able to update cart quantity accordingly with the text value on the form. I was able to handle deleting and insertion, the only part that is left the batch update, I will appreciate any assistance. I have done a few research, but was unable to handle this.

What I have tried:

The Post Method:
C#
[HttpPost]
    public ActionResult ViewCart(IEnumerable<string> quantity, string command,IEnumerable<string> hiddens)
    {

        if (command == "UpdateCart")
        {
            var getRecordForUpdate = (from u in db.ShoppingCarts.Where(x => x.CartSessionID == Request.AnonymousID) select u).ToList();
            if(getRecordForUpdate !=null)
            {
                foreach (var quan in quantity)
                {
                    get
                }
            }              
        }
}
Posted
Updated 9-Aug-16 5:02am
v3
Comments
Karthik_Mahalingam 9-Aug-16 10:35am    
Uwakpeter 9-Aug-16 11:30am    
I don't know Jquery, i will appreciate if you could further assist.

See my solution below:

Controller / Models
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CartSample.Controllers
{
    public class CartController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            var model = new Order
            {
                Products = new List<Product>
                {
                    new Product
                    {
                        Name = "item 1",
                        Qty = 2,
                        Price = 9.99m
                    }
                }
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(Order model)
        {
            // Do something with udpated quantities
            foreach(var product in model.Products)
            {
                UpdateProductQty(product);
            }
            return View(model);
        }

        private void UpdateProductQty(Product product)
        {
            // Do something more useful than this example...
            var updatedQuantity = product.Qty;
        }
    }
    public class Order
    {
        public List<Product> Products { get; set; }
        public Order()
        {
            Products = new List<Product>();
        }
    }

    public class Product
    {
        public string Name { get; set; }
        public int Qty { get; set; }
        public decimal Price { get; set; }
        public decimal Total()
        {
            return Price * Qty;
        }
    }
}


View

Note: how I have used Html.hiddenFor to ensure the built in MVC mechanism maps the form fields for existing cart items to the model parameter for the Index (post) action in the controller.
That way you can easily look in the model submitted to action post and do what you want with updated Qty for every item.

HTML
@model CartSample.Controllers.Order
<html>
<head><title>Cart Sample</title>
</head>

<body>
    @using (Html.BeginForm("Index", "Cart", FormMethod.Post))
    {
        <table>
            <tr>
                <th>Name</th>
                <th>Qty</th>
                <th>Price</th>
                <th>Total</th>
            </tr>
            @for (int i = 0; i < Model.Products.Count; i++)
            {
                <tr>
                    <td>@Html.HiddenFor(x=>x.Products[i].Name)
                        @Html.DisplayFor(x=>x.Products[i].Name)</td>
                    <td>@Html.TextBoxFor(x=>x.Products[i].Qty)</td>
                    <td>@Html.HiddenFor(x => x.Products[i].Price)
                        @Html.DisplayFor(x => x.Products[i].Price)</td>
                    <td>@Model.Products[i].Total()</td>
                </tr>
            }
            <tr>
                <td>
                    <input type="submit" value="update" />
                </td>
            </tr>
        </table>
    }
</body>
</html>


This results in the following screens
https://s9.postimg.org/puc32qwdr/Cart.png[^]
 
Share this answer
 
v2
Comments
Uwakpeter 9-Aug-16 11:36am    
would this still work if one has more than one items in the cart?
njammy 9-Aug-16 11:44am    
It absolutely works with many items.
By the way this will work async as well if you replace my code "beginForm" with Ajax form.
Change your controller's action where you want to perform action like this

C#
[HttpPost]
    public ActionResult ViewCart(int Id)
    {
       var getRecordForUpdate = (from u in db.ShoppingCarts.Where(x =>x.CartSessionID == Id) select u).FirstOrDefault();
            if(getRecordForUpdate !=null)
            {
                //do your stuff here
            }              
        }
}


This will help you to make action for the single selected value or model whatever you are binding.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900