2

I'm using EPiServer version 11 and I have requirement that when property of type linkItemCollection is rendered using PropertyFor() method, I need to add some custom attribute ( based on condition if target is blank ) to generated hyperlink.

@Html.PropertyFor(x => x.Layout.LinksCollection)

I have idea of creating a custom view under DisplayTemplates in view and adding new view. My query is how can i get default template for linkItemCollection to get it started ?

user1641519
  • 377
  • 1
  • 4
  • 17

3 Answers3

1

The easy option would be to o it yourself and not worry about the Property for, the only slight issue is that you may not get inline editing to work.

https://www.jondjones.com/learn-episerver-cms/episerver-developers-tutorials/episerver-properties/how-to-display-a-list-of-links-in-episerver/

To go with your route

[UIHint("MyView")]
[Display(
    GroupName = SystemTabNames.Settings,
    Order = 100)]
public virtual LinkItemCollection MyProperty{ get; set; }

In Views/Shared/DisplayTemplates add a template MyView.cshtml

Jon Jones
  • 1,014
  • 1
  • 9
  • 17
0

Instead of using the PropertyFor you could take full control of the rending yourself.

    // FullRefreshPropertiesMetaData asks on-page edit to reload the page 
    // to run the following custom rendering again after the value has changed.
    @Html.FullRefreshPropertiesMetaData(new []{ "RelatedContentLinks" })

    // EditAttributes enables on page-edit when you have custom rendering.
    <p @Html.EditAttributes(m => m.CurrentPage.RelatedContentLinks) >
    @if (Model.CurrentPage.RelatedContentLinks != null)
    {        
        <span>See also:</span>
        foreach (LinkItem item in Model.CurrentPage.RelatedContentLinks)
        {
            <a href="@UrlResolver.Current.GetUrl(item.Href)">@item.Text</a>        }
    }
    </p>

Taken from the EPi documentation

Asthiss
  • 221
  • 2
  • 8
0

Thanks for your input on this.

I managed to resolve this as below.

 public static MvcHtmlString LinkItemCollectionFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            MvcHtmlString result = PropertyExtensions.PropertyFor(html, expression);
            return MvcHtmlString.Create(result.ToString().Replace("target=\"_blank\"", "target=\"_blank\" rel=\"noopener noreferrer\""));
        }

Hope, it helps someone.

user1641519
  • 377
  • 1
  • 4
  • 17