samedi 9 mai 2015

Asp.net MVC Controller Refresh add same value to session everytime

I am trying to create a shopping cart in asp.net MVC. I write the following code in my controller to get the select item from the product list to my cart.

public ActionResult OrderNow(int id) { decimal? total;

        if (Session["cart"] == null)
        {
            List<Item> cart = new List<Item>();
            cart.Add(new Item(db.Products.Find(id),1));
              foreach(var result in cart)
              {
                  //ViewBag.price = result.Pr.UnitPrice * 1;
                  total = cart.Sum(item => item.Pr.UnitPrice);
                  ViewBag.price = total;
                  Session["total"] = total;
                  result.Quantity = 1;
              }
            Session["cart"] = cart;
        }
        else
        {
            List<Item> cart = (List<Item>)Session["cart"];
            for (int i = 0; i < cart.Count; i++)
                if (cart[i].Pr.ProductID == id)
                {
                    cart[i].Quantity++;
                    total = cart.Sum(item => item.Pr.UnitPrice);
                    ViewBag.price = total;
                    Session["total"] = total;
                }

                else
                {
                    cart.Add(new Item(db.Products.Find(id), 1));
                    total = cart.Sum(item => item.Pr.UnitPrice);
                    ViewBag.price = total;
                    Session["total"] = total;
                }

            //cart.Add(new Item(db.Products.Find(id), 1));
            Session["cart"] = cart;

        }
        return View("Cart");
    }

here is the view for my shopping cart

<table class="table table-condensed">
                <thead>
                    <tr class="cart_menu">
                        <td class="image">Item</td>
                        <td class="description"></td>
                        <td class="price">Price</td>
                        <td class="quantity">Quantity</td>
                        <td class="total">Total</td>
                        <td></td>
                    </tr>
                </thead>
                <tbody>
                    @foreach(Item item in (List<Item>)Session["cart"])
                    { 
                    <tr>
                        <td class="cart_product">
                            <img src="@Url.Content(item.Pr.Picture1)" alt="" width="75px" height="75px"  />
                        </td>
                        <td class="cart_description">
                            <h4><a href="">@item.Pr.ProductName</a></h4>
                            <p>Product ID: @item.Pr.ProductID</p>
                        </td>
                        <td class="cart_price">
                            <p>$@item.Pr.UnitPrice</p>
                        </td>
                        <td class="cart_quantity">
                            <div class="cart_quantity_button">
                                @*<a class="cart_quantity_up" href=""> + </a>*@
                                @Html.TextBoxFor(Model => item.Quantity , new { @class = "cart_quantity_input", @size = "2" })
                                @*<input class="cart_quantity_input" type="text" name="quantity" value="1" autocomplete="off" >*@
                                @*<a class="cart_quantity_down" href=""> - </a>*@
                            </div>
                        </td>
                        <td class="cart_total">
                            <p class="cart_total_price">$@(item.Pr.UnitPrice * item.Quantity)</p>
                        </td>
                        <td class="cart_delete">
                            <a class="cart_quantity_delete" href=""><i class="fa fa-times"></i></a>
                        </td>
                    </tr>
                    }
                </tbody>
            </table>

but I am getting an issue in this code whenever I refresh the page its add one more item in my cart and if I select the one product its add the product next row not add count to items..

I hope you understand what I want to ask.. Anybody can resolve the issue ?

Aucun commentaire:

Enregistrer un commentaire