我想更改 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&#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/