@Html.Raw control with htmlencode

(ASP.NET MVC)


Hi,
In this article i am discussing about the @html.Raw control. I am reading the string from viewbag and i replace the "\n" new line character with html "<br/>" line break then rendered as html using @html.raw

View:-

@{
    ViewBag.Title = "HtmlRaw";
}

<h2>HtmlRaw</h2>
@Html.Raw(Server.HtmlEncode(@ViewBag.Message).Replace("\n", "<br/>"))

@Html.Raw(HttpUtility.HtmlEncode(@ViewBag.Message).Replace("\n", "</br>"))


Controller:-

  public ActionResult HtmlRaw()
        {

            ViewBag.Message = "C\nC++\nC#\nJava Script";
            return View();
        }

Image

Ratings


Average Rating: 5.00 by 13 users
Giri Prasad
7/1/2013
7/1/2013
Download PDF

Comments






Jerry

Very good article!!


Katie

Very nice!