Render html from sql - webmatrix

眉间皱痕 提交于 2019-12-02 03:27:40

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/32038586/render-html-from-sql-webmatrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!