Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to implement Checkout functionality in my shopping cart application and i am having trouble with re-using a session variable. The session variable represents Order total and is defined in the Shopping Cart razor view . The variable is working well and increments appropriately whenever i add an item to the cart. The user will click Proceed to Checkout upon finishing the adding items and then this redirects to Checkout page where the Order total should also be shown.

The Checkout page is showing a value of 0 even though the session variable has a value (from Cart view)

HTML
<pre> @{
    int sum = 0;
        }

        @{
            Session["OrderTotal"] = sum;
        }
<div class="container-fluid">
    <div class="row">
        <div class="col-12 col-lg-8">
            <div class="cart-title mt-50">        
            </div>

            <div class="cart-table clearfix">
                <table class="table table-responsive" tabindex="1" style="overflow: hidden; outline: none;">
                    <thead>
                        <tr>                         
                            <th>Product</th>                         
                            <th>Price</th>
                            <th>Quantity</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                        <tr>
                                                   <td class="cart_product_desc">
                                <h5>@item.Quantity</h5>
                            </td>
                            <td>

                                <button id="" class="" data-slno="@item.ProductId" onclick="location.href='@Url.Action("Remove","Home",item)'">
                                    <img src="~/assets/img/core-img/xxx.png" height="30" width="30" />
                                </button>
                            </td>
                        </tr>
                        }
                    </tbody>

                    @foreach (var x in Model)
                    {

                        sum = Convert.ToInt32(x.Price) + sum;

                    }
                </table>
            </div>
        </div>
        <div class="col-12 col-lg-4">
            <div class="cart-summary">
                <h5>Cart Total</h5>
                <ul class="summary-table">
                    <li><span>subtotal:</span> <span>$0.00</span></li>
                    <li><span>delivery:</span> <span>Free</span></li>
                    <li><span>total:</span> <span>$@sum</span></li>
                </ul>
                <div class="cart-btn mt-100">
                    <span> <a href="~/Home/Checkout" class="btn amado-btn mb-15">Checkout</a></span> <span><a href="#" class="btn amado-btn mb-15">Continue Shopping</a></span>
                   
                    
                </div>
            </div>
        </div>
    </div>
</div>


What I have tried:

This is how im attempting to re-use the OrderTotal session variable in the Checkout view :

HTML
<pre>    @model IEnumerable<NdinemiEshop.Product>
        @{
            ViewBag.Title = "Cart";
            Layout = "~/Views/Shared/_LayoutCart.cshtml";
        }

<div class="container-fluid">
    <div class="row">
        <div class="col-12 col-lg-8">
            <div class="checkout_details_area mt-50 clearfix">

                <div class="cart-title">
                    <h2>Checkout</h2>
                </div>

                <form action="#" method="post">
                    <div class="row">
                        <div class="col-md-6 mb-3">

                            @Html.EditorFor(model => model.Initiator, new { htmlAttributes = new { @class = "form-control", @placeholder = "Buyer Name" } })
                            @Html.ValidationMessageFor(x => x.Initiator, "", new { @class = "text-danger" })
                            @*<input type="text" class="form-control" id="first_name" value="" placeholder="Sender Name" required="">*@
                        </div>
                        <div class="col-md-6 mb-3">

                            @Html.EditorFor(model => model.Recipient, new { htmlAttributes = new { @class = "form-control", @placeholder = "Recipient Name" } })
                            @Html.ValidationMessageFor(x => x.Recipient, "", new { @class = "text-danger" })
                            @*<input type="text" class="form-control" id="last_name" value="" placeholder="Recipient Name" required="">*@
                        </div>
                        <div class="col-12 mb-3">
                            @Html.EditorFor(model => model.CardNo, new { htmlAttributes = new { @class = "form-control", @placeholder = "Card No" } })
                            @Html.ValidationMessageFor(x => x.CardNo, "", new { @class = "text-danger" })
                            @*<input type="text" class="form-control" id="company" placeholder="Company Name" value="">*@
                        </div>
                        <div class="col-12 mb-3">
                            @Html.EditorFor(model => model.CvvNo, new { htmlAttributes = new { @class = "form-control", @placeholder = "Cvv No" } })
                            @Html.ValidationMessageFor(x => x.CvvNo, "", new { @class = "text-danger" })
                            @*<input type="email" class="form-control" id="email" placeholder="Email" value="">*@
                        </div>
                        <div class="col-12 mb-3">
                            @Html.EditorFor(model => model.Expires, new { htmlAttributes = new { @class = "form-control", @placeholder = "Card Expiration" } })
                            @Html.ValidationMessageFor(x => x.Expires, "", new { @class = "text-danger" })
                            @*<input type="email" class="form-control" id="email" placeholder="Email" value="">*@
                        </div>
                     
                      

                      
                </form>
            </div>
        </div>
        <div class="col-12 col-lg-4">
            <div class="cart-summary">
                <h5>Cart Total</h5>
                <ul class="summary-table">
                    <li><span>subtotal:</span> <span>$140.00</span></li>
                    <li><span>delivery:</span> <span>Free</span></li>
                    <li><span>total:</span> <span>$@Session["OrderTotal"]</span></li>
                </ul>

              
            </div>
        </div>
    </div>
</div>


What am i missing?
Posted
Updated 14-Oct-20 4:00am

1 solution

Quote:
C#
int sum = 0; // Set the local variable "sum" to zero;
Session["OrderTotal"] = sum; // Store zero in the session variable;
...
foreach (var x in Model)
{
    // Update the local variable - doesn't touch the session:
    sum = Convert.ToInt32(x.Price) + sum; 
}
You aren't storing the sum in the session.

Move the line which sets the session variable to the end of the view.

Better yet, move the calculation and session access to the action, and keep it out of the view. Or have your checkout action load the total from the same data store as your cart action.
 
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