簡體   English   中英

將@ Html.Raw轉換為ASP.NET MVC 4 Razor中的字符串

[英]Convert @Html.Raw to string in 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);
}

另請注意,您不想修剪轉義的字符串。 您永遠都不知道要修剪什么。 這里的解決方案可以正確執行。

其作品:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM