【发布时间】:2014-10-15 08:11:53
【问题描述】:
我有一个模型文件,它返回一个带有换行符 BR 标记的 HTML 字符串,但是如何在浏览器上显示该 HTML?问题在于换行符,标签本身显示在 UI 上
我试图将模型放在 Html.Raw(modelItem => item.Speaking) 中,但它永远不会工作,因为它期望里面有一个字符串,并且无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型
下面是我尝试过的代码和cmets。
<div>
@{
string strTest = "<br/>Line 1 <br/> Line 2<br>";
@Html.Raw(strTest); //This works and display as expected
@MvcHtmlString.Create(strTest); //This works and display as expected
@Html.Raw(Html.DisplayFor(modelItem => item.Speaking)); //This doesn't work, its show the <br /> on the screen
@MvcHtmlString.Create(Html.DisplayFor(modelItem => item.Speaking).ToString()); //This doent work, its show the <br /> on the screen
@Html.Raw(modelItem => item.Speaking) //This throw error Cannot convert lambda expression to type string
}
</div>
感谢任何帮助或建议。提前致谢!
【问题讨论】:
-
我的答案改了,看看吧。
标签: asp.net-mvc razor asp.net-mvc-5 html-helper