c# - 将 @Html.Raw 转换为 ASP.NET MVC 4 Razor 中的字符串

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

我想要的是将输出 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/

相关文章:

html - 我怎样才能将这个 DIV 恰好放在中间并且在调整浏览器大小时它不移动?

javascript - 单击按钮时触发图像像素

android - 使用 flexbox 的网站无法在 IOS 上运行

javascript - 父子记录与 HTML 表的关系

c# - WPF 的有效拖放启用 ListView 实现?

c# - TCP 服务器 CPU 使用率高

asp.net-mvc - 通过 MVC4 中的表单发布 JSON 数据

jquery - $.getJSON 不调用 "successful"函数

c# - 上下文在 Code First 模式下使用,代码是从 EDMX 文件生成的

c# - 如何增加数据检索?