2

I'm using cshtml pages with webmatrix, i'm trying to render the html that is stored in my db, but the output is like <b>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </b> instead of Lorem ipsum dolor sit amet, consectetur adipiscing elit. (This is just an example to explain what is happening)

I'm storing in ntext datatype.

Here is me code.

  @{
   var db = Database.Open("myDB");
   var selectQueryString = "SELECT * FROM noticias ORDER BY id";
   }
 <!DOCTYPE html>
   <html lang="en">
      <head>
         <meta charset="utf-8" />
         <title></title>
          <style>
           p {color: #f00;}
         </style>
     </head>
     <body>

        @foreach(var row in db.Query(selectQueryString)){

               <p>@row.header</p>
               <p>@row.description</p>

       }

    </body>
  </html>
2
  • write into controller for retrieve data from db and pass as model into view, better create a DAO class and use the DAO class into controller. Follow best practices, follow asp.net/mvc/overview/getting-started/… Commented Aug 16, 2015 at 18:43
  • @JahirulIslamBhuiyan This is ASP.NET Web Pages, not MVC. There is no controller or model. asp.net/web-pages Commented Aug 18, 2015 at 15:42

1 Answer 1

3

The syntax with '@' automatically applies encoding of HTML sensitive characters. If your variables (or method call, etc.) returns a string that contains HTML markup and you want the browser to render that markup, wrap the string in a MvcHtmlString.

Any of these will work:

@Html.Raw(row.header)
@(new MvcHtmlString(row.header))
@MvcHtmlString.Create(row.header)

Note that this will expose the browser to ANY HTML markup contained in the values. This method should only be used if the data is verified to only contain safe markup, no scripts, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok i see your point but i can't figure it to my example. And yes the data will have no scripts.
Nevermind mate, was looking to the wrong side. It's workin with @Html.Raw, just had to change my code and now it's working. Thanks a lot for your time.
MvcHtmlString is not available in the Web Pages framework. Html.Raw is the correct option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.