I\'m using Razor in MVC 3 and Asp.net C#.
I have a View with the following code. model.ContentBody
has some HTML tags.
I would need display this HTM
@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));
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);
}
Use this code in the controller:
string noHTML = Regex.Replace(inputHTML, @"<[^>]+>| ", "").Trim();
<div class="display-field">
@Html.Raw(Model.ContentBody)
</div>
This code solved the problem!
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>
Please use to display Html use with decode.
@MvcHtmlString.Create(@Model.OurVision)