c# - MvcHtmlString.ToString 正在转义 Razor 中的文本

标签 c# asp.net-mvc razor

我想更改 DisplayFor 输出(格式化字符串),但输出字符串有一个 & 符号。如何处理 MvcHtmlString 以不转义 & 符号?

这是 cshtml 文件中的代码:

@{
    string title = @Html.DisplayFor(model => model.Entity.Description).ToString();
    if (Model.City != null)
        title = String.Format("{0} em {1}", title, Model.Entity.CityName);
    ViewBag.Title = title;
}

model.Entity.Description 在数据库中有值 "Automóvel"。但输出在 html 中呈现为 Automóvel。这是 html 输出:

<p>Autom&amp;#243;vel</p>

如果我尝试在 ViewBag.Title 中使用 Html.Raw(),则不会显示标题。

编辑:添加更多细节

即使我尝试使用此代码的建议方法,ViewBag.Title 也不会显示它。这是注释后修改后的代码:

@{
    string title = Model.Entity.Description;
    if (Model.City != null)
    { 
        title = String.Format("{0} em {1}", Model.Entity.Description,
            Model.Entity.CityName);
    }
    ViewBag.Title = Html.Raw(HttpUtility.HtmlDecode(title));
}

最佳答案

你需要解码它。

 @Html.Raw(HttpUtility.HtmlDecode(model.Entity.Description));

关于c# - MvcHtmlString.ToString 正在转义 Razor 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31479092/

相关文章:

c# - 如何将具有多种像素格式的多页 tiff 保存为单个像素格式?

c# - 反序列化为没有 XML 容器元素的列表

javascript - 如何在 JavaScript : ASP. NET Core 中使用模型数据

asp.net-mvc - 用于高速 Web 应用程序开发的 Django、Scala Lift 或 ASP.NET-MVC?

spring - 用 Thymeleaf 扩展视野

c# - 将 <text> 转换为字符串?

C#、不变性和公共(public)只读字段

ASP.NET MVC - 重定向到外部页面?

c# - 将 css 样式表的内容呈现为内联样式属性

c# - 是否可以从 Controller 方法以编程方式调用 Razor 编译器?