Copied to clipboard

Flag this post as spam?

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


  • steschu 88 posts 489 karma points
    Jul 17, 2019 @ 15:09
    steschu
    0

    property textstring numeric characters automatically convert to real number

    Hi,

    I have a textstring property where I put some version numbers in it (e.g. "1.0"). When I query the model I noticed that somehow automatically the string is regarded as a number, because the value appears as "1" (without the . and 0) on the page. In the model cs file I can see that the property is generated as type of object. This may be the reason. But how could this be solved?

    Kind regards, Stephan

  • David Challener 80 posts 444 karma points c-trib
    Jul 17, 2019 @ 15:35
    David Challener
    1

    Hi Stephan,

    This probably depends on how you're retrieving the property. Are you using models builder or using GetPropertyValue() ?

    You can specify a type with GetPropertyValue eg

    Model.Content.GetPropertyValue<string>("textstringPropertyAlias")
    

    If using models builder that's a bit strange, but would take a look at the type it has created and change the model (depending what mode you're running) - need more info from you here.

    HTH, David

  • steschu 88 posts 489 karma points
    Jul 17, 2019 @ 20:34
    steschu
    100

    Hi David,

    Forgot to mention that the textstring comes from a custom property editor. I forgot to implement a Property Value Converter. That was the reason. Now everything works fine. Here's the documentation (https://our.umbraco.com/documentation/Extending/Property-Editors/value-converters) and my implementation below:

    using System;
    using Umbraco.Core;
    using Umbraco.Core.PropertyEditors;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    
    namespace Plugins.MyTextbox
    {
        [PropertyValueType(typeof(String))]
        [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
        public class MyTextboxPropertyConverter : IPropertyValueConverter
        {
            public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
            {
                return Convert.ToString(source);
            }
    
            public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
            {  
                return source;
            }
    
            public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
            {
                return source.ToString();
            }
    
            public bool IsConverter(PublishedPropertyType propertyType)
            {
                return propertyType.PropertyEditorAlias.Equals("MyCustom.MyTextbox");
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft