0

I am trying to figure out how to apply "Shipping charges are $3.99 for the first item and $.99 for each additional item" in my Cart View.

Right now I have

@{
       double itemTotal = 0;
       double subTotal = 0;
       int totalQty = 0;
       double discount = 0.8;
       double shippingBase = 3.99;
       double shippingItem = 0.99;
       double totalShipping = 0;
}

@foreach (var item in Model)
    {

            double price = (double)item.price / 100 * discount;
            itemTotal = @item.qty * price;
            subTotal += itemTotal;
            totalQty += @item.qty;

I am not sure how to approach this. Would I use a foreach loop to count the total quantitys and if the quantity is greater that one it would add 3.99+99???

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Timothy Wong
  • 95
  • 1
  • 1
  • 14
  • 3
    Better do this calc in a controller and return an appropriate ViewModel. Do not use view for business logic – Steve Feb 24 '17 at 21:14
  • The calculus of shipping cost can be done in a Cart class. Cart class can contain the property Items of type IEnumerable and a property ShippingCost where is implemented the logic defined for shipping computation. Then you define in the CartViewModel class another IEnumerable to keep the items to render in the view using the @foreach and a property ShippingCost of type decimal where to store the cost of shipping. The controller create the instance of CartViewModel class and set Items and ShippingCost. – Ghini Antonio Feb 24 '17 at 21:40
  • Are charges based on different items or total quantity? It is not clear as the stated charge is different after first `item`. but an item has a quantity. – Nkosi Feb 24 '17 at 22:20

1 Answers1

0

As I have said in a comment at your question, the business logic has no place inside a view. Better do the calc in a controller and return the result in a specific Model for your view, or in case of a simple values you can use the ViewBag to transfer this data from your controller to the view.

In your case I would make the calc in the controller using a simple trick

decimal itemTotal = 0m;
decimal subTotal = 0m;
int totalQty = 0;
decimal discount = 0.8m;
decimal shippingItem = 3.99m;
decimal totalShipping = 0m;

foreach (var item in YourEnumerableItems)
{
    decimal price = item.price / 100 * discount;
    itemTotal = item.qty * price;
    subTotal += itemTotal;
    totalQty += item.qty;
    totalShipping += shippingItem;

    // first shipping cost added, now set it to 0.99m for next items
    shippingItem = 0.99m;
}
ViewBag.Total = subTotal;
ViewBag.Quantity = totalQty;
ViewBag.Shipping = totalShipping;

And the view could use the ViewBag values for its display....

Notice also that for calculating money values you should always use the decimal datatype to avoid rounding errors implicit in the floating point datatype.

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • The calculations are still incorrect. It is not adding the addtional 0.99 for each item – Timothy Wong Feb 24 '17 at 21:39
  • 1
    Let me understand. the shipping charges are 3.99 for the first item, then from the second one they are 0.99 right? I suggest you to use the debugger. Put a breakpoint (F9) on the foreach line, then run the app until the breakpoint is it. At this point continue step by step (F10) and check what happens inside the loop. – Steve Feb 24 '17 at 21:49