Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 593 posts 2389 karma points
    Jul 31, 2016 @ 11:39
    Bo Jacobsen
    1

    Custom Property Value Converter

    Hello Everyone.

    I am stuck tryin' to make a Custom Property Value Converter. That has to convert Json data from my Plugin to custom Object called MyLookAlikeArchetypeModel.

    What i wanna be able to do is this

    @using myOwnDll.Something
    var obj = Model.Content.GetPropertyValue<MyLookAlikeArchetypeModel>("propertyAlias")
    @foreach(var item in obj) 
    { 
        <p>@item.Name</p>
        <p>@item.Value</p>
    }
    

    what is got is this

    using System.Collections.Generic;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Core.PropertyEditors;
    using Newtonsoft.Json;
    
    public class MyLookAlikeArchetypeFieldSet
    {
        [JsonProperty("name")]
        public string Name { get; private set; }
    
        [JsonProperty("nalue")]
        public string Value { get; private set; }
    }
    
    // Its this one i cant figure out 'I think.'
    public class MyLookAlikeArchetypeModel : List<MyLookAlikeArchetypeFieldSet> {}
    
    public class MyLookAlikeArchetypeModelPropertyConverter : IPropertyValueConverter
    {
        public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
        {
            return source;
        }
    
        public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
        {
            try
            {
                return JsonConvert.DeserializeObject<List<MyLookAlikeArchetypeFieldSet>>(source as string);
            }
            catch
            {
                return null;
            }
        }
    
        public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
        {
            return null;
        }
    
        public bool IsConverter(PublishedPropertyType propertyType)
        {
            return propertyType.PropertyEditorAlias.Equals("My.Plugin.Package.Manifest.Alias");
        }
    }
    

    I hope someone can help.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 31, 2016 @ 13:45
    Alex Skrypnyk
    0

    Thanks, Bo, great example.

    Alex

  • Bo Jacobsen 593 posts 2389 karma points
    Jul 31, 2016 @ 15:00
    Bo Jacobsen
    0

    Unfortunately do my exsample not work. But i hoped someone could help.

  • Bo Jacobsen 593 posts 2389 karma points
    Jul 31, 2016 @ 16:18
    Bo Jacobsen
    100

    Json data would be

    [ 
       { "name": "A name", "value": "A value" }, 
       { "name": "Another name", "value": "Another value" }
     ]
    

    And i got it working with this

    using System.Collections.Generic;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Core.PropertyEditors;
    using Newtonsoft.Json;
    using System.Linq;
    
    public class MyLookAlikeArchetypeFieldSet
    {
        [JsonProperty("name")]
        public string Name { get; set; }
    
        [JsonProperty("value")]
        public string Value { get; set; }
    }
    
    public class MyLookAlikeArchetypeModel
    {
        private List<MyLookAlikeArchetypeFieldSet> _Items;
    
        public MyLookAlikeArchetypeModel()
        {
            _Items = new List<MyLookAlikeArchetypeFieldSet>();
        }
    
        public MyLookAlikeArchetypeModel(List<MyLookAlikeArchetypeFieldSet> list)
        {
            _Items = list;
        }
    
        public IEnumerator<MyLookAlikeArchetypeFieldSet> GetEnumerator()
        {
            return _Items.GetEnumerator();
        }
    
        public bool Any()
        {
            return _Items.Any();
        }
    }
    
    public class MyLookAlikeArchetypeModelPropertyConverter : IPropertyValueConverter
    {
        public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
        {
            return source;
        }
    
        public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
        {
            try
            {
                var list = JsonConvert.DeserializeObject<List<MyLookAlikeArchetypeFieldSet>>(source as string);
                return new MyLookAlikeArchetypeModel(list);
            }
            catch
            {
                return new MyLookAlikeArchetypeModel();
            }
        }
    
        public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
        {
            return null;
        }
    
        public bool IsConverter(PublishedPropertyType propertyType)
        {
            return propertyType.PropertyEditorAlias.Equals("My.Plugin.Package.Manifest.Alias");
        }
    }
    

    now i can use what i wanted.

    var obj = Model.Content.GetPropertyValue<MyLookAlikeArchetypeModel>("propertyAlias")
    @if(obj.Any())
    {
        foreach(var item in obj)
        {
            <p>@item.Name</p>
            <p>@item.Value</p>
        }
    }
    

    If anyone got a better solution. Please let me know!

  • Gayathri 55 posts 175 karma points
    Jun 14, 2017 @ 03:18
    Gayathri
    0

    i got an error like this

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

  • Gayathri 55 posts 175 karma points
    Jun 14, 2017 @ 04:44
    Gayathri
    0

    this is my code

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.HomePage>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using System.Web.Script.Serialization;
    @using Newtonsoft.Json.Linq;
    @using System.Collections.Generic;
    @using Umbraco.Core.Models.PublishedContent;
    @using Umbraco.Core.PropertyEditors;
    @using Newtonsoft.Json;
    @using System.Linq;
    
    
    @{
      Layout = "Master1.cshtml";
    }
    
    @{
    
        if (CurrentPage.HasValue("promo"))
        {
            var promoListValue = CurrentPage.GetPropertyValue("promo");           
    
    
                var obj = Model.Content.GetPropertyValue<MyLookAlikeArchetypeModel>("promo");
                <span>@obj </span>
                 foreach(var item in obj)
                    {
                        <p>@item.img</p>
                        <p>@item.alias</p>
                    }
    
        } 
    
    
    
    }
    
    
    
    <div class="container _borderless landingpage">
    
    </div>
    @functions {
    
    
    
    public class MyLookAlikeArchetypeFieldSet
    {
        [JsonProperty("alias")]
        public string alias { get; set; }
    
        [JsonProperty("img")]
        public string img { get; set; }
    }
    
    public class MyLookAlikeArchetypeModel
    {
        private List<MyLookAlikeArchetypeFieldSet> _Items;
    
        public MyLookAlikeArchetypeModel()
        {
            _Items = new List<MyLookAlikeArchetypeFieldSet>();
        }
    
        public MyLookAlikeArchetypeModel(List<MyLookAlikeArchetypeFieldSet> list)
        {
            _Items = list;
        }
    
        public IEnumerator<MyLookAlikeArchetypeFieldSet> GetEnumerator()
        {
            return _Items.GetEnumerator();
        }
    
        public bool Any()
        {
            return _Items.Any();
        }
    }
    
    public class MyLookAlikeArchetypeModelPropertyConverter : IPropertyValueConverter
    {
        public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
        {
            return source;
        }
    
        public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
        {
            try
            {
                var list = JsonConvert.DeserializeObject<List<MyLookAlikeArchetypeFieldSet>>(source as string);
                return new MyLookAlikeArchetypeModel(list);
            }
            catch
            {
                return new MyLookAlikeArchetypeModel();
            }
        }
    
        public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
        {
            return null;
        }
    
        public bool IsConverter(PublishedPropertyType propertyType)
        {
            return propertyType.PropertyEditorAlias.Equals("My.Plugin.Package.Manifest.Alias");
        }
    }
    }
    

    but the obj returning as empty

     var obj = Model.Content.GetPropertyValue<MyLookAlikeArchetypeModel>("promo");
    

    the response for CurrentPage.GetPropertyValue("promo");

     [ { "alias": "1", "content": "1", "img": "/media/1069/509253678.jpg" }, { "alias": "Slide 2", "content": "2", "img": "/media/1074/636609180.jpg" } ] 
    

    any help will be appreciated

  • Bo Jacobsen 593 posts 2389 karma points
    Mar 11, 2021 @ 15:16
    Bo Jacobsen
    0

    Hi Gayathri.

    I think you need a check on your obj is empty before rendering it.

  • brahim19 7 posts 27 karma points
    Mar 10, 2021 @ 14:46
    brahim19
    0

    I have exactly the umbra8,but that doesn't work.does anyone have any idea why

  • Bo Jacobsen 593 posts 2389 karma points
    Mar 11, 2021 @ 15:18
    Bo Jacobsen
    0

    Hi brahim19

    This link is for valueconverters for Umbraco 8.

    https://our.umbraco.com/Documentation/Extending/Property-Editors/value-converters

  • brahim19 7 posts 27 karma points
    Mar 12, 2021 @ 16:01
Please Sign in or register to post replies

Write your reply to:

Draft