I have a property of a model class that contains a relative url to a file.
~/_docs/folder/folder/document.pdf
How I can, in the view, transform it to an hyperlink to download the file itself?
thanks
I have a property of a model class that contains a relative url to a file.
~/_docs/folder/folder/document.pdf
How I can, in the view, transform it to an hyperlink to download the file itself?
thanks
<a href="<%= Url.Content("~/_docs/folder/folder/document.pdf") %>">
document.pdf
</a>
Or to make this more elegant and avoid the spaghetti code you could write a custom html helper:
public static class HtmlExtensions
{
public static MvcHtmlString ContentLink(
this HtmlHelper htmlHelper,
string linkText,
string contentPath,
object htmlAttributes
)
{
var a = new TagBuilder("a");
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
a.MergeAttribute("href", urlHelper.Content(contentPath));
a.MergeAttributes(new RouteValueDictionary(htmlAttributes));
a.SetInnerText(linkText);
return MvcHtmlString.Create(a.ToString());
}
}
and then:
<%= Html.ContentLink(
"download.pdf",
"~/_docs/folder/folder/document.pdf",
new { title = "Download download.pdf" }
) %>