Dynamically resolving component links in views using DD4T - not within rich text fields using ResolveRichText()
- but if component A has a component link field with a link to component B and within your view you want to render a url to the page component B is published on ...
There's no helper for this in the solution - is that right?
Bit rusty with ASP.NET MVC 3, so purists look away, but the following works, I just need to create the 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);
}
Just though it was a bit strange there's no helper already in the project for this.
Cheers
There is indeed no HTML helper for resolving a link. Main reason is probably that (according to the MVC principles) the link should already be resolved in the Model your view is rendering.
If you checkout the ContentModel class from the dd4t.ContentModel project, there is a (commented out) property 'ResolvedUrl' for a component. This is never implemented, but it is a more approperiate place for resolving the link. But your code does the job, so feel free to implement your own HTML helper.