Reputation: 341
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
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
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>
Appreciate any help or suggestions. thanks in advance!
Upvotes: 13
Views: 39323
Reputation: 7259
You can just omit both DisplayFor and modelItem =>, so:
@Html.Raw(item.Speaking)
Upvotes: 7
Reputation: 2189
In MVC4
Instead of using @Html.Raw(modelItem => item.Speaking)
You can use
@Html.Raw(@Model.Speaking.ToString())
It works for me and I hope this help someone else too
Upvotes: 11
Reputation: 1116
Try this :
@(new HtmlString(stringWithMarkup))
and you can create a HTML helper too!:
@helper RawText(string s) {
@(new HtmlString(s))
}
Upvotes: 15