问题
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