Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • atze187 160 posts 215 karma points
    Feb 23, 2011 @ 10:27
    atze187
    0

    Output version numbers stored in textstring properties

    Hi,

    I just installed umbraco 4.7 beta (assembly version: 1.0.4071.3793) to take advantage of Razor-based macros. Great work guys, but I got stuck:

    I store version numbers (e.g. 1.1) in text fields using a validator that ensures proper format when editing those values. When I output those values using Razor these are interpreted as decimals and loose the dots, so 1.1 will become 11, which makes it completely useless for me.

    Any advice (apart from storing major and minor version in separate properties)?

    Cheers,
    Atze

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 23, 2011 @ 10:34
    Sebastiaan Janssen
    0

    Strange.. Have you tried doing something like 

    @Model.MyPropety.ToString()
  • atze187 160 posts 215 karma points
    Feb 23, 2011 @ 11:23
    atze187
    0

    Sorry, forgot to mention: tried that too.

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 23, 2011 @ 11:33
    Sebastiaan Janssen
    0

    Alright, let's go all out then:

    @{
        Decimal version = 0;
        if(Decimal.TryParse(Model.MyProperty, out version))
        <p>@version</p>
        }
    }

    This shouldn't be necessary and ToString() should just work, but I'd be curious to see if this helps.

    Also, what does this say:

    @Model.MyProperty.GetType().ToString()
  • atze187 160 posts 215 karma points
    Feb 23, 2011 @ 11:47
    atze187
    0

    Seems like I have found the cause. The ConvertPropertyValueByDataType method in umbraco.MacroEngines.DynamicNode makes a decimal.TryParse().

  • atze187 160 posts 215 karma points
    Feb 23, 2011 @ 11:55
    atze187
    0

    @Sebastiaan: First snippet results in "The best overloaded method match for 'decimal.TryParse(string, out decimal)' has some invalid arguments" as the first parameter is already decimal, as the second snippet proves by returning System.Decimal.

  • atze187 160 posts 215 karma points
    Feb 23, 2011 @ 12:04
    atze187
    0

    I think I will go for the datatype stuff introduced with v4.5 and make a custom datatype.

    But anyway, thanks for your effort!

    Atze

  • Jonas Eriksson 930 posts 1825 karma points
    Feb 23, 2011 @ 12:04
    Jonas Eriksson
    0

    not an answer - but note that with the new System.Web.WebPages.dll in umbraco we have some useful String extensions at hand, for example: var s="12.23"; decimal d = s.AsDecimal();

    http://msdn.microsoft.com/en-us/library/system.web.webpages.stringextensions%28v=vs.99%29.aspx

  • Jonas Eriksson 930 posts 1825 karma points
    Feb 23, 2011 @ 12:06
    Jonas Eriksson
    2

    But - you can still use the "good old" GetProperty(propertyAlias).Value:

    @Model.GetProperty("MyProperty").Value
  • atze187 160 posts 215 karma points
    Feb 23, 2011 @ 12:22
    atze187
    0

    Thanks Jonas, saved my day, for shizzle!

  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 23, 2011 @ 12:54
    Gareth Evans
    0

    Yes correct, the new razor stuff will attempt to cast to decimals/ints/bools etc as otherwise everything is strings and you end up having to do all sorts of types etc.

    I hadn't considered version numbers would get interpreted as decimals.

    The suggestion of using .GetProperty is probably the best

Please Sign in or register to post replies

Write your reply to:

Draft