get text from PageLink helper

Vote:
 
#151154
Jul 12, 2016 15:54
Vote:
 

Dont think that htmlhelper exist out-of-the-box in Episerver.  

Here is an example of one to get you started:

public static class HtmlHelperExtensions
    {
        public static MvcHtmlString PageLinkText(this HtmlHelper htmlHelper, ContentReference pageLink,  object htmlAttributes, string htmltag = "span")
        {
            if (ContentReference.IsNullOrEmpty(pageLink))
                return MvcHtmlString.Empty;
            var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
            var tagBuilder = new TagBuilder(htmltag??"span");
            var page = contentLoader.Get<PageData>(pageLink);
            tagBuilder.InnerHtml = HttpUtility.HtmlEncode(page.PageName);
            var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            tagBuilder.MergeAttributes(attributes);
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
        }
    }

...and you can now use it in your .cshtml like:

@Html.PageLinkText(Model.CurrentPage.PageLink,new {@class="cssclass-without-link"})

Added the possiblility to send along attributes for your containing element and also configure what that element should be. 

Another option is of course to add this to your viewmodel instead and resolve the text for the link in the controller instead using pretty much the same code. 

#151172
Jul 12, 2016 22:35
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.