How to calculate the total cost of a basket from an embossing pattern mvc cart

I am trying to get the mkc knockout structure. I look at a sample shopping basket and try to figure out:

  • How to calculate the total cost
  • Where to add business rules on the client side (for example, discounts and vouchers)

To calculate the subtotal, the code reads

@using (lines.If(m => m.ProductId != -1))
{
    using (var product = lines.With(m => ko.Model.DataBase[m.CategoryId].Products[m.ProductId]))
    {
        @product.Html.Span(m => "\\$" + m.Price)                  
    }
}

When I try to get the total amount from there, I usually end up with a compiler exception or a NullReferenceException at runtime. for instance

@using (lines.If(m => m.ProductId != -1))
{
    using (var product = lines.With(m => ko.Model.Categories[m.CategoryId].Products[m.ProductId]))
    {
        @product.Html.Span(m => "\\$" + (lines.Model.Quantity * m.Price))                                                       
        @{double total = lines.Model.Quantity * m.Price;}
    }
}

Gives me

Compiler error message: CS1501: overload for 'Write' method takes 0 Arguments

Looks like I'm doing it wrong. Will someone point me in the right direction?

+5
source share
1

@ ? , , @. , , "double total" {}?

@using (var product = lines.With(m => ko.Model.Categories[m.CategoryId].Products[m.ProductId]))
{
    product.Html.Span(m => "\\$" + (lines.Model.Quantity * m.Price));
    double total = lines.Model.Quantity * m.Price;
}
0

All Articles