c# - ViewModel 中的 HTML.Raw 等效 DataAnnotation

标签 c# asp.net asp.net-mvc asp.net-mvc-4 razor

有没有等同于@Html.Raw(Model.Property)的注解在 ViewModel 中?

我正在尝试使用 [DisplayFormat(HtmlEncode=false)]未能成功创建 HTML 标记(<b><span>),而是将其显示为文本。

最佳答案

您可以使用 UIHint 通过创建一个模板 Views\Shared\DisplayTemplates\Html.cshtml 来做到这一点:

@model string
@Html.Raw(Model)

然后在你的模型上:

[UIHint("Html")]
public string MyHtml {get; set;}

这可以在所有其他 View 中输出为:

@Html.DisplayFor(m => m.MyHtml)

关于c# - ViewModel 中的 HTML.Raw 等效 DataAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703242/

相关文章:

c# - 有没有像样的 ADO.NET Helper 实用程序?

C# 如何用直引号代替微软的Smart Quotes?

c# - 在 Windows 10 中将应用程序注册到 URI 方案

asp.net-mvc - 如何在MVC6或AspNet Core或IdentityCore中更改PasswordValidator

javascript - 为什么我的 mvc ajax 找不到操作

c# - 数据未在循环中保存在 ModelList 变量中。 "Index was out of range"

javascript - 从 html View 中的 onchange 事件调用 Controller 函数

c# - 如何在 ViewComponent Asp.Net Core 中获取当前登录用户

asp.net - 如何找到有关System.Runtime.InteropServices.COMException的引用? (0x800A08FE)

asp.net - 带有 col、colgroup、tbody 和 thead 的 HTML 表标记在 Visual Studio 2010 和 Visual Studio 2012 中引发编译错误