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

Fla*_*vio 1 html c# asp.net-mvc razor asp.net-mvc-4

我想要的是将输出IHtmlString转换为String.

我有这个代码:

string text = @Html.Raw(Model.lastNoticias.Descricao);
Run Code Online (Sandbox Code Playgroud)

此代码返回错误:

无法将System.Web.IHtmlString类型隐式转换为字符串.

完整代码:

@{ 
    string text = @Html.Raw(Model.lastNoticias.Descricao);
}
@if (text.Length > 100)
{
    @(text.Substring(0, 100) + "... ");
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Kad*_*dir 7

@if (Model.lastNoticias.Descricao.Length > 100)
{
    @Html.Raw(Model.lastNoticias.Descricao.Substring(0, 100) + " ...");
}
else
{
    @Html.Raw(Model.lastNoticias.Descricao);
}
Run Code Online (Sandbox Code Playgroud)

另请注意,您不希望修剪转义字符串.你永远不知道你在修剪什么.这里的解决方案是正确的.