ErocM
ErocM

Reputation: 4662

How do I output html from my database

I am displaying my 'news' page and I want the customer to be able to output some simple html.

My view looks like this:

@using SuburbanCustPortal.SuburbanService

    <br />

    @foreach (var item in (IEnumerable<TokenNews>) ViewBag.News)
    {
      <div class="fulldiv">
        <fieldset> 
          <legend>@item.Title</legend>
          <div>
            @item.Body
          </div>    
        </fieldset>
      </div>
    }

When I do it this away, the html isn't being rendered by the browser, it's just showing the html as text.

Any way that I can output the html where the browser will render it?

Upvotes: 0

Views: 135

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65116

You didn't exactly specify what part is being shown as text, but if it's item.Body, do @Html.Raw(item.Body) instead. That turns a string into an IHtmlString, whose purpose is to tell Razor that this thing is guaranteed to be safe to output as-is, and will not contain nasties like XSS attacks (ensuring this when using Html.Raw is your job). Everything that is not an IHtmlString will be escaped automatically by Razor.

Upvotes: 5

Related Questions