I've created a custom property (addressFinder), with a valueType "JSON", and 3 properties in the JSON object (addressLatitude, addressLongitude, and locationAddress). Here's the package.manifest:
I'm using the data type in a document type called 'Office Location' where I'm wanting admin users to enter map locations that I'm going to use for a single Google map on another page.
I'm trying to render out an array of the location data from addressFinder on another page (a document type called 'Map'), and was just wondering how I access the stored JSON data? I'm actually using a partial view macro file within a page, but I'm assuming it's the same syntax.
I tried both of the following to no avail (just to see if I could get a value printed out):
Is anyone able to point me in the right direction in terms of accessing the fields within my JSON object?
You'll probably want to implement a custom property convertor.
This would handle deserializing the JSON into an object that you can use in your partial view (and anywhere else on the site that might use the same data type).
Chris pointed me in the right direction, as I was also searching for the same issue you have Victoria. With some research and digging trough the Umbraco core I've come up with the following (which is working for me):
You create a custom converter class, where you need to make sure to give the correct name of the property type alias (which you define in the package.manifest - in my case "myPropertyTypeAlias")
public class MyCustomerPropertyValueConverter : IPropertyValueConverter
{
public bool IsConverter(IPublishedPropertyType propertyType)
{
return propertyType.Alias == "myPropertyTypeAlias";
}
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 propertyType.GetType();
}
public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
{
return PropertyCacheLevel.Snapshot;
}
public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
{
return MyCustomModel.Deserialize(source as string);
}
public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
return inter;
}
public object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
return inter?.ToString() ?? string.Empty; ;
}
}
Then you have your model MyCustomModel:
public class MyCustomModel
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("latitude")]
public double Latitude { get; set; }
[JsonProperty("langitude")]
public double Langitude { get; set; }
public static MyCustomModel Deserialize(string json)
{
if (json == null || !json.StartsWith("{") || !json.EndsWith("}")) return null;
return JsonConvert.DeserializeObject<MyCustomModel>(json);
}
}
And in your (partial)view you simply call it like this:
Rendering custom properties in a template
Hey there,
I've created a custom property (addressFinder), with a valueType "JSON", and 3 properties in the JSON object (addressLatitude, addressLongitude, and locationAddress). Here's the package.manifest:
I'm using the data type in a document type called 'Office Location' where I'm wanting admin users to enter map locations that I'm going to use for a single Google map on another page.
I'm trying to render out an array of the location data from addressFinder on another page (a document type called 'Map'), and was just wondering how I access the stored JSON data? I'm actually using a partial view macro file within a page, but I'm assuming it's the same syntax.
I tried both of the following to no avail (just to see if I could get a value printed out):
Is anyone able to point me in the right direction in terms of accessing the fields within my JSON object?
Many thanks,
Victoria
You'll probably want to implement a custom property convertor.
This would handle deserializing the JSON into an object that you can use in your partial view (and anywhere else on the site that might use the same data type).
There is a good example of how to do that in the following forum post : https://our.umbraco.com/forum/umbraco-7/developing-umbraco-7-packages/61760-A-question-about-property-converters#comment-210057
Hope that helps!
Chris pointed me in the right direction, as I was also searching for the same issue you have Victoria. With some research and digging trough the Umbraco core I've come up with the following (which is working for me):
You create a custom converter class, where you need to make sure to give the correct name of the property type alias (which you define in the
package.manifest
- in my case "myPropertyTypeAlias")Then you have your model
MyCustomModel
:And in your (partial)view you simply call it like this:
I hope this helps. Keep in mind, that I did this with Umbraco 8.1.0.
is working on a reply...