<div class="panel-body">@Html.Raw(item.PostContent.Substring(0, 200))</div>
上面的代码产生错误!,当我将鼠标悬停在
item.PostContent
上时,它告诉我它是String!而这段代码:
<div class="panel-body">@Html.Raw(item.PostContent)</div>
工作正常,并显示整个帖子内容!
我应该怎么做才能解决这个问题?
我想获取帖子的前200个字符,并将其显示为摘录。
最佳答案
如下所示可能会有用。
public static class HtmlPostExtensions
{
public static IHtmlString Post(this HtmlHelper helper, string postContent)
{
string postStr = postContent;
if (postStr.Length > 200)
{
postStr = postStr.Substring(0, 200);
}
return MvcHtmlString.Create(postStr);
}
}
可以用作
@Html.Post(item.PostContent)