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 = {};
}
}
});
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;
}
}
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----------
-------------------------------javascript_code--
----------------------manifest_code
----------------------------htmlproprety_code
---------------homehtml_code-----------
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:Or maybe the
.Nam
or.Valu
are null.But from what i can see your
IPropertyValueConverter
is converting to aIPublishedContent
and you expect aMyLookAlikeArchetypeModel
and that might be problem. So you might have to do this.is working on a reply...