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; }
}
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 decimal
s 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; }
}
}