Hi Guys,
As you all know that some time we need to define custom properties model and then use those properties in Episerver CMS and for that we are using PropertyList.
But PropertyList doesn't support complex property and we need custom implementation to support this.
Today, I am going to use Episerver URL inside my PropertyList class and then use it to render the link on front-end.
So for this first we need to define our custom PropertyList model class
using System.ComponentModel.DataAnnotations;
using EPiServer;
namespace Testing.Models.Blocks
{
public class GridLink
{
public virtual string Title { get; set; }
[Display(Name = "Url")]
public virtual Url Url { get; set; }
}
}
You noticed that i am using Episerver URL class and defined a property called Url.
Now i have to create a PropertyDefinition class to use it on my block/page. You can find info here
using Testing.Models.Blocks;
using EPiServer.Core;
using EPiServer.PlugIn;
namespace Testing.Business.PropertyDefinition
{
[PropertyDefinitionTypePlugIn]
public class GridLinksProperty : PropertyList { }
}
You can use this model on our block/page as a property.
[CultureSpecific]
[Display(
Name = "List of Links",
Description = "Heading field's description",
GroupName = SystemTabNames.Content,
Order = 200)]
[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor))]
public virtual IList ListOfLinks { get; set;
Now if you are going create a block/page of this and try to save it. It will remove the URL field value on publish
Now if you want use this property in a proper manner then you have to update your "GridLink" model and decorate the Url property with following attribute.
[JsonProperty]
[JsonConverter(typeof(UrlConverter))]
So now you model will look like this-
using System.ComponentModel.DataAnnotations;
using EPiServer;
using EPiServer.Cms.Shell.Json.Internal;
using Newtonsoft.Json;
namespace Testing.Models.Blocks
{
public class GridLink
{
public virtual string Title { get; set; }
[JsonProperty]
[JsonConverter(typeof(UrlConverter))]
[Display(Name = "Url")]
public virtual Url Url { get; set; }
}
}
That's it now you loop through your model property and render this url property in regular manner.
<a href="@Url.ContentUrl(link.Url)" > </a >
Thanks for reading this blog post I hope it helps
Thanks and regards
Ravindra S. Rathore