DD4T and dynamic linking

Dynamically resolving component links in views using DD4T - not in extended text boxes using ResolveRichText()

- but if component A has a component link field with a link to component B and in your view, you want to render the page url of component B published to ...

There is no helper in the solution - is that correct?

Bit rusty with ASP.NET MVC 3 so purists are looking aside but the following works, I just need to create an Html Helper.

@using DD4T.Providers.SDLTridion2011sp1;

@{
    var linkFactory = new LinkFactory();
    linkFactory.LinkProvider = new TridionLinkProvider();
    var link = linkFactory.ResolveLink(Model.Component.Fields["related_link"].LinkedComponentValues[0].Id);
}

      

It's just that although it was a little weird, there is no helper in this project.

Greetings

+3


source to share


3 answers


There is actually no HTML helper for resolving the link. The main reason is probably because (according to MVC principles) the link should already be resolved in the model that your render represents.



If you select the ContentModel class from the dd4t.ContentModel project, the component will have (commented out) a "ResolvedUrl" property. This is never implemented, but this is a better place to resolve the link. But your code does the job, so feel free to implement your own HTML helper.

+6


source


I think you are right, it should be within the framework. Fortunately, this is very easy to add. Just create a helper class using an extension method like this:

namespace MyApp.Helpers
{
    public static class ModelHelper
    {
        public static string GetResolvedUrl(this IComponent component)
        {
            return GetResolvedUrl(component, null, null);
        }
    }
}

      

Now, if you make sure your view is using the MyApp.Helpers namespace, you can do this in your component views:



@model DD4T.ContentModel.Component
@using MyApp.Helpers
<a href="@Model.GetResolvedUrl()">click here</a>

      

We will most likely include this in the framework shortly.

+9


source


The good part about adding your own extension method is that you have the option to do something extra.

For example, we use it to differentiate between components. Multimedia.Url and LinkFactory.ResolveLink (component.Id) depending on the weather, the component is a multimedia component or a regular component (since multimedia components such as a PDF file are usually not placed on the page, so dynamic link resolution will not return the result to you.

+2


source







All Articles