剃刀以DisplayFor DataType.Url,在新窗口中打开 [英] Razor view to DisplayFor DataType.Url that opens in new window

查看:98
本文介绍了剃刀以DisplayFor DataType.Url,在新窗口中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么做与剃刀和DisplayFor一个新窗口中打开链接?

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

我有一个属性是 DataType.Url

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

使用 @ Html.DisplayFor 将此转换为 A 标记。

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

不过,我需要它来打开一个新窗口。设置新目标{=_空白} 不添加属性的目标。

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

这只是这一个网页,我需要开放的功能,在一个新的窗口,以便使用JavaScript似乎有点过分。

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?

推荐答案

您可以通过在添加文件〜/查看/共享/ DisplayTemplates / Url.cshtml 包含以下内容:

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>

这篇关于剃刀以DisplayFor DataType.Url,在新窗口中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆