Search code examples
c#htmlsqlasp.net-mvchtml-encode

How do i display html tags from sql serever database to html page?


i want to display html tags which are in my database to a html webpage. Example:

this is the first post

(with the p tags) - this is stored in database with datatype of varchar(max). when i display this on webpage it displays it but not in html just in text. what am i doing wrong? When i inspect the element i see this <p>this is the first post</p>


Solution

  • I believe you need to encapsulate your item with Html.Raw() as in for example:

    @model IEnumerable<SOL.Models.Myhtmls>
    @foreach (var item in Model)
    {
        @Html.Raw(item.tag) //where tag is your model's property
    }