I use the Html.Raw to print a raw html content, for example when I send some thing like ViewBag.div = "<div> Hello </div>";
from the controller to the view side it does not print a raw html content unless I use the Html.Raw
method but if I have an encoded content, like content encoded using jquery and inserted into the database and I want to print it as a raw html content the Html.Raw
method does not work and I have to use HttpUtility.HtmlDecode(EncodedContent)
before I use Html.Raw
so please could anyone explain why it acts in this way and what is the proper situation to use Html.Raw
method? or in another way, why Html.Raw
does not work when it receives html entities as a parameter instead of html tags?.
The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project.
What is HTML Helper in ASP.NET MVC 5? HTML Helpers are methods that return a string. Helper class can create HTML controls programmatically. HTML Helpers are used in View to render HTML content. It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application.
Inline HTML Helpers @helper syntax is used to create reusable inline helper method. They make the code more reusable, as well as more readable. Let's see how to use them. Create an empty ASP.Net MVC Application. Right click the controller and add a controller.
Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.
Because encoded characters are HTML, and the Raw version of that string is the encoded one.
Html.Raw renders what it is given without doing any html encoding, so with ViewBag.div = "<div> Hello </div>";
:
@Html.Raw(ViewBag.div);
Renders
<div> Hello </div>
However, when you have encoded characters in there, such as ViewBag.Something = ">";
the raw version of that is >
. To get back to actual html you need to Html.Raw(HttpUtility.HtmlDecode(EncodedContent));
as you've said.
If Html.Raw did do the decoding then it would be confusing, and we would need something that didn't do it. ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With