umco / umbraco-ditto

Ditto - the friendly view-model mapper for Umbraco

Home Page:http://our.umbraco.org/projects/developer-tools/ditto

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decimal mapping always 0 for negative values

roadhousedev opened this issue · comments

A Decimal in Umbraco and decimal in the C# model maps correctly when the data value is greater than 0 but negative values map as 0.

Example:
1.56 > 1.56m (correct)
0 > 0m (correct)
-5.2 > 0m (incorrect)

Hey Roadhousedev,
Which versions of Ditto & Umbraco are you using? Also could you possibly provide a snippet of your C# model?

I had a quick play with Umbraco 7.5.8 and Ditto 0.10 (latest) and aside from the Umbraco Decimal field losing some precision (-5.2 becomes -5 out of the box) it yielded -5.

My test was using the following code.

public class MyViewModel
{
    [UmbracoProperty]
    public decimal NumericProperty { get; set; }
}

In Umbraco I have a property called "numericProperty" which uses the default Numeric data type.

Thanks,
Jamie

Hi Jamie,

I am just doing a standard mapping using:

IPublishedContent node = new UmbracoHelper(UmbracoContext.Current).TypedContent(result.id);
Offer offer = node.As<Offer>();

and the Offer class is something like:
public class Offer : Master
{
public string Description { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}

It does work when I change Latitude from a Decimal to a Numeric on the Document Type, but I too lose the precision doing that.

Umbraco 7.5.7
Ditto 0.8.4

That sounds more like an Umbraco bug to me than a Ditto one. All we will be doing internally is calling the API. What does GetPropertyValue yield you?

Like @JimBobSquarePants says, it could be an Umbraco issue. I've downgraded Ditto to 0.8.4 with Umbraco 7.5.8 and I have the same results as with 0.10.

So checking the outcome of the following code would be useful to working out where the problem lies.

IPublishedContent node = new UmbracoHelper(UmbracoContext.Current).TypedContent(result.id);

var latitude = node.GetPropertyValue<decimal>("latitude");
var longitude = node.GetPropertyValue<decimal>("longitude");

Note that GetPropertyValue<T>(string) is an extension method that requires using Umbraco.Web;.

Hope this helps us get a bit closer to the answer!

Just to chime in... Ditto (or the UmbracoProperty processor rather) would be doing is this...

var prop = node.GetProperty("alias");
var value = prop.Value;

Ditto doesn't try to cast the type - whereas GetPropertyValue<T> would attempt to cast it to T (using Umbraco's TryConvertTo<T> extension method.


Update! Urgh, sorry scrap that - I was mistaken... we do call GetPropetyValue, just not the generic version of it. Sorry for any confusion.

It looks like it's an Umbraco bug that is also occurring for GetProperty(). Notice that DataValue has its string populated with "-5.6" yet Value is 0. See screenshot: https://i.imgur.com/qE1ry3f.png

I have reported this issue here: http://issues.umbraco.org/issue/U4-9464

Thanks for confirming. Let's close this then as it's not specific to Ditto.