How to display encoded HTML as decoded in MVC 3 Razor?
<div class="display-field">
@Html.Raw(Model.ContentBody)
</div>
This code solved the problem!
You could also decode html in the controller before sending the model to the view,
WebUtility.HtmlDecode()
public ActionResult Index(int id)
{
var content = _contentRepository.GetContent(id);
var classViewModel = new ClassViewModel
{
ContentBody = WebUtility.HtmlDecode(ClassViewModel.ContentBody)
};
return View(classViewModel);
}
Well, to summarize.. In my service/Controller, while returning the model to the view
....
Description = WebUtility.HtmlDecode(narration.Narration1)
My cshtml, where the tinyMCE is displayed.. just bind regularly (I was using HtmlAttributeHelper for other purpose. You can ignore it)
<div>
@Html.TextAreaFor(model => model.Description, HtmlAttributeHelper.ConditionalDisable(false, new {@class = "tinymce"}))
</div>
And in the cshtml page where the data is displayed..
......
<td class="col-word-wrap">@Html.Raw(HttpUtility.HtmlDecode(@item.Narration1))</td>
@Html.Raw
was not work for me here is example:-
string name = "<p><span style="font-size: small; color: #ff0000;"><span style="font-size: small;">&nbsp;<span style="font-size: large; color: #000000;">Hi</span><br />&nbsp; <br />This is just a sample,<br />This will not work with @Html.Raw(),<br />";
<span>@Html.Raw(name);</span>
But this worked instead:-
@MvcHtmlString.Create(HttpUtility.HtmlDecode(@model.ContentBody))
or you can also use :-
@Html.Raw(HttpUtility.HtmlDecode(@model.ContentBody));