我想要的是将输出 IHtmlString 转换为 String。
我有这个代码:
string text = @Html.Raw(Model.lastNoticias.Descricao);
此代码返回错误:
无法将类型 System.Web.IHtmlString 隐式转换为字符串。
完整代码:
@{
string text = @Html.Raw(Model.lastNoticias.Descricao);
}
@if (text.Length > 100)
{
@(text.Substring(0, 100) + "... ");
}
我该怎么做?
最佳答案
@if (Model.lastNoticias.Descricao.Length > 100)
{
@Html.Raw(Model.lastNoticias.Descricao.Substring(0, 100) + " ...");
}
else
{
@Html.Raw(Model.lastNoticias.Descricao);
}
另请注意,您不想修剪转义字符串。你永远不知道你在修剪什么。这里的解决方案是正确的。
关于c# - 将 @Html.Raw 转换为 ASP.NET MVC 4 Razor 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613767/