Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor view to DisplayFor DataType.Url that opens in new window

How do I make links open in a new window with Razor and DisplayFor?

I have a property which is DataType.Url,

[DataType(DataType.Url)]
public string SiteUrl { get; set; }

Using @Html.DisplayFor this is converted into an a tag.

@Html.DisplayFor(model => item.SiteUrl)

However I need it to open in a new window. Setting new {target="_blank"} does not add the attribute for target

@Html.DisplayFor(model => item.SiteUrl, new {target="_blank" })

It is just this one page where I need the functionality of opening in a new window so using JavaScript seems a bit excessive.

How do I get the links to open in a new window?

like image 773
Simon Martin Avatar asked May 20 '13 11:05

Simon Martin


1 Answers

You could override the default template by adding a file in ~/Views/Shared/DisplayTemplates/Url.cshtml with the following contents:

<a href="@ViewData.Model" target="_blank">@ViewData.TemplateInfo.FormattedModelValue</a>
like image 154
Darin Dimitrov Avatar answered Nov 16 '22 14:11

Darin Dimitrov