With the release of ASP.NET MVC 3 Release Candidate 2 (RC2) you can finally use Html.Raw when you don’t your output to be encoded.
So this means you don’t have to use
@MvcHtmlString.Create(ViewBag.HtmlOutput)
or
@(new HtmlString(ViewBag.HtmlOutput))
or anything else to output a string containing HTML in ASP.NET MVC.
An example of using Html.Raw in ASP.NET MVC 3 using Razor
The code below shows the HTML string to be outputted being added the dynamic ViewBag collection.
public class HomeController : Controller
{
public ActionResult Index()
{
StringBuilder htmlOutput = new StringBuilder();
htmlOutput.Append("
The p_w_picpath below is from " );
htmlOutput.Append("Wikimedia Commons");
htmlOutput.Append("
");htmlOutput.Append("");
ViewBag.HtmlOutput = htmlOutput.ToString();
return View();
}
}
Which can be used in a view like this
@{
ViewBag.Title = "Index";
}
@Html.Raw(ViewBag.HtmlOutput)
to show the following output in a browser