5

I am trying to get my head around knockout mvc framework. I am looking at the sample of a shopping cart and trying to figure out:

  1. How to calculate total cost
  2. Where to add client side business rules (such as discounts and vouchers)

To calculate 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 from there I usually end up with a compiler exception or NullReferenceException in run time. For example

@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: No overload for method 'Write' takes 0 arguments

Seems like I am doing it wrong. Would anyone point me in a right direction?

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • Which line does it point at with this error? Furthermore, is this your first attempt at using KO in general or just the KO MVC framework? – Matthew Cox Sep 15 '13 at 00:15

1 Answers1

0

Have you tried removing the @ symbol from the start of each line? I'm sure once you open a razor code block, you don't need to keep prepending every line with @. Also, not sure why the 'double total' line is wrapped in {}?

@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;
}
sambo
  • 221
  • 1
  • 6