2

KnockoutMVC 2.10, MVC 4.0, C# 5.

Working from one of the examples on the main site (Computed fields in Sub Models). I am having a problem and wondered if anyone could help. In the code below, the computed Message field updates fine, based on two text boxes associated with Caption and Value respectively. However, as soon as I un-comment the second [Computed] attribute, with no other changes to the View (or any other code), it stops working. Incidentally, in the same project, in the main model I have tried 2 computed fields and they worked fine. Is this a limitation of sub models (ie only one computed field allowed)?

Thanks Rob

public class InnerComputedSubModel
{
    public decimal Caption { get; set; }
    public decimal Value { get; set; }
    public decimal Caption2 { get; set; }
    public decimal Value2 { get; set; }

    [Computed]
    public decimal Message
    {
        get { return Caption * Value; }
    }

    //[Computed]
    public decimal Message2
    {
        get { return Caption2 * Value2 * 20; }
    }
}

public class InnerComputedModel
{
    public InnerComputedSubModel SubModel { get; set; }
}
Rob L
  • 2,124
  • 3
  • 22
  • 50

1 Answers1

2

KnockoutMVC does support multiple Computed properties however it has some bugs when using decimal values inside Computed properties.

One possible workaround is to don't use decimals in your Computed but float or double away there is no JavaScript equivalent of the C# decimal type.

So the following code should work fine:

public class InnerComputedSubModel
{
    public double Caption { get; set; }
    public double Value { get; set; }
    public double Caption2 { get; set; }
    public double Value2 { get; set; }

    [Computed]
    public double Message
    {
        get { return Caption * Value; }
    }

    [Computed]
    public double Message2
    {
        get { return Caption2 * Value2 * 20; }
    }
}
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Nemesv. Thank you very much for the quick response. I never would have known about the issue with decimal. That has fixed my issue and allowed me to move on with my project. – Rob L Jun 10 '13 at 02:06