Copied to clipboard

Flag this post as spam?

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


  • brahim19 7 posts 27 karma points
    Mar 10, 2021 @ 12:55
    brahim19
    0

    Custom Property Value Converter umbraco8

    could you please see the code.i made the code.but when at home.cshtml wants to call.comes an error .(System.NullReferenceException: "Object reference not set to an instance of an object.").

    ------------------------convert_code----------

     public class MyLookAlikeArchetypeFieldSet
        {
    
    [JsonProperty("nam")]
    public string Nam { get; set; }
    
    [JsonProperty("valu")]
    public string Valu { 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();
        }
    
    }
    
    namespace MyConverters
    {
        public class ContentPickerPropertyConverter : IPropertyValueConverter
        {
            private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
        //Injecting the PublishedSnapshotAccessor for fetching content
        public ContentPickerPropertyConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor)
        {
            _publishedSnapshotAccessor = publishedSnapshotAccessor;
        }
    
        public bool IsConverter(IPublishedPropertyType propertyType)
        {
            return propertyType.EditorAlias=="Mypropretyeditor";
        }
    
        public bool? IsValue(object value, PropertyValueLevel level)
        {
            switch (level)
            {
                case PropertyValueLevel.Source:
                    return value != null && (!(value is string) || string.IsNullOrWhiteSpace((string)value) == false);
                default:
                    throw new NotSupportedException($"Invalid level: {level}.");
            }
        }
    
        public Type GetPropertyValueType(IPublishedPropertyType propertyType)
        {
            return typeof(IPublishedContent);
        }
    
        public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
        {
            return PropertyCacheLevel.Elements;
        }
    
        public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
        {
            if (source == null) return null;
    
            var attemptConvertInt = source.TryConvertTo<int>();
            if (attemptConvertInt.Success)
                return attemptConvertInt.Result;
    
            var attemptConvertUdi = source.TryConvertTo<Udi>();
            if (attemptConvertUdi.Success)
                return attemptConvertUdi.Result;
    
            return null;
        }
    
        public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            if (inter == null)
                return null;
    
            if ((propertyType.Alias != null) == false)
            {
                IPublishedContent content;
                if (inter is int id)
                {
                    content = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(id);
                    if (content != null)
                        return content;
                }
                else
                {
                    var udi = inter as GuidUdi;
                    if (udi == null)
                        return null;
                    content = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(udi.Guid);
                    if (content != null && content.ContentType.ItemType == PublishedItemType.Content)
                        return content;
                }
            }
    
            return inter;
        }
        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 ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            if (inter == null) return null;
            return inter.ToString();
        }
        }
    }
    

    -------------------------------javascript_code--

    angular.module("umbraco").controller("MypropertyeditorControll", function ($scope) {
        $scope.init = function () {
           // alert("Der Controller ist gelandet");
            if ($scope.model.value === undefined || $scope.model.value === "") {
                $scope.model.value = {};
            }
        }
    
    });
    

    ----------------------manifest_code

    {
    
        propertyEditors: 
        [ 
            {  
                alias: "Mypropretyeditor",
    
                name: "My proprety editorE",
    
                icon: "icon-smiley-inverted",
    
                group: "media",
    
                editor: { 
    
                    view:"~/App_Plugins/MarkDownEditor/Myproprety.html" ,
                    valueType :"JSON"
    
                }
    
            } 
        ],  
        javascript : ['~/App_Plugins/MarkDownEditor/JS/Mypropretyeditor.controlle.js' 
        ],  
        css : ['~/App_Plugins/MarkDownEditor/CSS/Mypropretyeditor.Style.css' 
        ]  
    }
    

    ----------------------------htmlproprety_code

    <div ng-controller="MypropertyeditorControll" ng-init="init()">
    
        <input type="text" ng-model="model.value.object.key1" />
        <input type="text" ng-model="model.value.object.key2" />
    
    </div>
    

    ---------------homehtml_code-----------

    @{
        Layout = null;
        //Url.GetCropUrl(mediaItem: Model.Image, cropAlias: "Grid"​, htmlEncode: true);
        var obj = Model.Value<MyLookAlikeArchetypeModel>("Mypropretyeditor");
    
    }
    
     <div>@foreach(var item in obj)
            {
            <p>@item.Nam</p>
            <p>@item.Valu</p>
            }</div>
    
  • Bo Jacobsen 607 posts 2405 karma points
    Mar 24, 2021 @ 13:25
    Bo Jacobsen
    0

    Hi Brahim.

    There are multiple null reference posibilities.

    Let's start with your .cshtml. You might need a reference to MyLookAlikeArchetypeModel like @using myNamespace.MyLookAlikeFolder in the top.

    The obj might be null and therefore cant Iterate over items. Therefore you might need a check like:

    @if(obj != null && obj.Any())
    {
        <div>
            @foreach (var item in obj)
            {
                <p>@item.Nam</p>
                <p>@item.Valu</p>
            }
        </div>
    }
    

    Or maybe the .Nam or .Valu are null.

    But from what i can see your IPropertyValueConverter is converting to a IPublishedContent and you expect a MyLookAlikeArchetypeModel and that might be problem. So you might have to do this.

    public class ContentPickerPropertyConverter : IPropertyValueConverter
        {
            public bool IsConverter(IPublishedPropertyType propertyType)
            {
                return propertyType.EditorAlias == "Mypropretyeditor";
            }
    
            public bool? IsValue(object value, PropertyValueLevel level)
            {
                switch (level)
                {
                    case PropertyValueLevel.Source:
                        return value != null && (!(value is string) || string.IsNullOrWhiteSpace((string)value) == false);
                    default:
                        throw new NotSupportedException($"Invalid level: {level}.");
                }
            }
    
            public Type GetPropertyValueType(IPublishedPropertyType propertyType)
            {
                return typeof(MyLookAlikeArchetypeModel);
            }
    
            public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
            {
                return PropertyCacheLevel.Elements;
            }
    
            public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType 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 ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
            {
                return inter ?? null;
            }
    
            public object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
            {
                return inter;
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft