0

I want to update quantity data, for that, I used a controller, I debugged and I looked, in HTTPpost, the value of quantity value successfully update.but when debug pointer goes to HTTPget method then it not updated and again I found the old quantity value. enter image description here

enter image description here

for these outputs my code is:

   [HttpGet]
        public IActionResult Cart()
        {
            List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");

            if (spray == null)
            {
                spray = new List<Spray>();
                HttpContext.Session.Set("spray", spray);
            }
            return View(spray);
        }

        [HttpPost]
        public IActionResult Cart(int id)
        {
            List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");

            var item = spray.SingleOrDefault(x => x.Id.Equals(id));
            if (item != null)
            {
                item.Quantity++;
            }

            return RedirectToAction(nameof(Cart));
        }

here I added an old quantity of my session data. this code is:


        [HttpGet]

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var product = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);

            if (product == null)
            {
                return NotFound();
            }

            return View(product);
        }




        [HttpPost]
        [ActionName("Details")]

        public ActionResult ProductDetails(int id,Spray s)
        {

            var r = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
            r.Quantity = s.Quantity;

            //List<Spray> spray = new List<Spray>();
            if (Utility.SessionExtensions.Get<List<Spray>>(HttpContext.Session, "spray") == null)
            {
                List<Spray> spray = new List<Spray>();
              
                spray.Add(r);
                r.Quantity = s.Quantity;

                Utility.SessionExtensions.Set(HttpContext.Session, "spray", spray);
            }

            else
            {
              
                List<Spray> spray = Utility.SessionExtensions.Get<List<Spray>>(HttpContext.Session, "spray");
                int index = isExist(id);
                if (index != -1)
                {
                    spray[index].Quantity++;
                }
                else
                {
                    var m = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
                    m.Quantity = s.Quantity;
                    spray.Add(m);
                }
                Utility.SessionExtensions.Set(HttpContext.Session, "spray", spray);
            }
            return RedirectToAction("Index");

           
        }

        private int isExist(int id)
        {
            List<Spray> spray = Utility.SessionExtensions.Get<List<Spray>>(HttpContext.Session, "spray");
            var r = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
            for (int i = 0; i < spray.Count; i++)
            {
                if (spray[i].Id.Equals(id))
                {
                    return i;
                }
            }
            return -1;
        }

Details.cshtml

<form method="post" asp-action="" enctype="multipart/form-data">
    <div class="row">
        <div class="col-1">

            <img src="~/@Model.Image" style="width:100%" onclick="myFunction(this);">
            </br></br>
            <img src="~/@Model.Image1" style="width:100%" onclick="myFunction(this);">
        </div>

        <div class="col-4 container">
            <span onclick="this.parentElement.style.display='none'" class="closebtn">&times;</span>
            <img id="expandedImg" style="width:100%">
            <div id="imgtext"></div>
        </div>
        <div class="col-4">
            <input type="hidden" asp-for="Id" />
            <h2>@Model.Name</h2>

            <p><b>@Model.Description</b></p>

            <h4>@Model.Price $</h4>
            <small class="text-danger">Clearence</small>
            </br>
            <hr>
            <h4>Size:@Model.Size</h4>
            <h6>Product Color:@Model.ProductColor</h6>
            </br></br></br></br>





            <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
            <div class="col-sm-12 col-xs-12 spinner-block">
                <div class="number-spinner">

                    <div class="input-group number-spinner">
                        <b class="mr-4"> <label asp-for="@Model.Quantity"></label></b></br>
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default btn-number btncartsniper" data-type="minus" data-dir="dwn"><span class="fa fa-minus fa-sm"></span></button>
                        </span>
                        <input asp-for="@Model.Quantity" class="form-control input-number Snippper_qty" value="0" type="number">
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default btn-number btncartsniper" data-type="plus" data-dir="up"><span class="fa fa-plus fa-sm"></span></button>
                        </span>
                    </div>
                </div>




            </div>
           
            
                <input type="submit" value="Add To Cart" class="btn btn-dark form-control" />
            
            @if (Model.Quantity > 0)
            {
                <h3>This Product Is InStock</h3>
            }
            else
            {
                <h1>Not InStock</h1>
            }
        </div>






    </div>







    <div>

        <a asp-action="Index" class="btn btn-dark">Back To List</a>
    </div>

</form>


Cart.cshtml

@using HuddsonBay.Models
@model List<Spray>
@{
    ViewData["Title"] = "Cart";
}

<h1>Your Cart</h1>
<br />



<div class="row">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Image</th>
                <th>Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Color</th>
                <th></th>
            </tr>
        </thead>

        <tbody>
            @foreach (var item in Model)
            {
            <tr>
                <td>
                    <img src="~/@item.Image" width="200px" height="150px" />
                </td>
                <td>@item.Name</td>
                <td>@item.Price</td>
                <td>@item.Quantity</td>
                <td>
                    <partial name="_QuantityPartial" model="@item.Id" />
                </td>

                <td>@item.ProductColor</td>
                <td>
                    <a asp-area="Customer" asp-action="Remove" asp-controller="SprayShow" asp-route-id="@item.Id" class="btn btn-danger">
                        <i class="fas fa-trash"></i>
                    </a>
                </td>

            </tr>
            }
        </tbody>
    </table>
</div>

<div class="row">
    <div class="col-6">
        <a asp-action="Index" asp-controller="SprayShow" class="btn btn-primary">Back To Home</a>
    </div>
    <div class="col-6 text-right">
        <h3>Total Amount</h3>
        <h3>Grand Total : @Model.Sum(c => c.Price)</h3>
        <a asp-area="Customer" asp-action="Checkout" asp-controller="Order" class="btn btn-info">Process To CheckOut</a>

    </div>
</div>









my output is: enter image description here

when I click the "+" button, then it responds goes to the cart "httpPost" here quantity value increase but when debug pointer goes cart "HTTPget" then I found the old value of the quantity.for that similarly, I can not find the updated quantity value of my output. I want to increase my quantity value.

I am a beginner. please help anyone.

2 Answers 2

1

The session data in ASP.NET Core is serialized, not as actual objects. This means when you get an object and modify it nothing is stored, since there’s no link with the serialized data in session store. You have to store the modified list back into the session after modifying anything in it.

See documentation

Sign up to request clarification or add additional context in comments.

Comments

0
    [HttpPost]
    public IActionResult Cart(int id)
    {
        List<Spray> spray = HttpContext.Session.Get<List<Spray>>("spray");

        var item = spray.SingleOrDefault(x => x.Id.Equals(id));
        if (item != null)
        {
            item.Quantity++;
           
            HttpContext.Session.Set("spray", spray); //it's must add for a set new session data
        }

        return RedirectToAction(nameof(Cart));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.