简体   繁体   English

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

[英]Convert @Html.Raw to string in ASP.NET MVC 4 Razor

What I want is to convert the output IHtmlString to String. 我想要的是将输出IHtmlString转换为String。

I have this code: 我有以下代码:

string text = @Html.Raw(Model.lastNoticias.Descricao);

This code return the error: 此代码返回错误:

Cannot implicitly convert type System.Web.IHtmlString to string. 无法将类型System.Web.IHtmlString隐式转换为字符串。

The full code: 完整代码:

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

How can I do it? 我该怎么做?

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

Also note that you don't want to trimmed an escaped string. 另请注意,您不想修剪转义的字符串。 You never know what you are trimming. 您永远都不知道要修剪什么。 The solution here does it correctly. 这里的解决方案可以正确执行。

其作品:

@{ string text = "any text"; } @Regex.Replace(text, @"<[^>]*>", "").Substring(0, 80);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM