从显示HTML模式中的字符串MVC的Razor视图 [英] Displaying HTML String from Model in MVC Razor View
问题描述
我有一个模型提起返回一个HTML字符串换行符BR标签,但我怎么显示HTML浏览器?问题INS而不是把换行,显示在用户界面上标记本身
I have a Model filed that returns an HTML string with line break BR tag, but How do I display that HTML on the browser ? The problem ins instead putting the line break, the Tag itself displaying on the UI
我试图把内Html.Raw(modelItem => item.Speaking)模型,但它永远不会奏效,因为它里面期待一个字符串,而不能转换拉姆达前pression键入线,因为它是不是委托类型
I tried to put the model within Html.Raw(modelItem => item.Speaking), but it never works as it expecting a string inside, and Cannot convert lambda expression to type 'string' because it is not a delegate type
下面是code和评论什么,我已经试过。
Below is the code and comments what I've tried.
<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>
鸭preciate任何帮助或建议。在此先感谢!
Appreciate any help or suggestions. thanks in advance!
推荐答案
试试这个:
@(new HtmlString(stringWithMarkup))
和您可以创建一个HTML帮助过!
and you can create a HTML helper too!:
@helper RawText(string s) {
@(new HtmlString(s))
}
这篇关于从显示HTML模式中的字符串MVC的Razor视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!