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");
}
}
[
{ "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!
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.
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
what is got is this
I hope someone can help.
Thanks, Bo, great example.
Alex
Unfortunately do my exsample not work. But i hoped someone could help.
Json data would be
And i got it working with this
now i can use what i wanted.
If anyone got a better solution. Please let me know!
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.
this is my code
but the obj returning as empty
the response for
CurrentPage.GetPropertyValue("promo");
any help will be appreciated
Hi Gayathri.
I think you need a check on your obj is empty before rendering it.
I have exactly the umbra8,but that doesn't work.does anyone have any idea why
Hi brahim19
This link is for valueconverters for Umbraco 8.
https://our.umbraco.com/Documentation/Extending/Property-Editors/value-converters
i have done so, but still this does not work. https://our.umbraco.com/forum/using-umbraco-and-getting-started/105347-custom-property-value-converter-umbraco8
is working on a reply...