StraightUp
StraightUp

Reputation: 225

Render rich text in MVC View?

I am creating an asp.net MVC application, and I am using rich text in order to store rich text inside my database.

While fetching the rich text from the database and displaying it on the Details page, I no longer see the formatted data. Rather I see the the plain html (for example < p>< b>< strong>rich text sample< /strong>< /b>< /p>).
How can I display rich text as it is formatted and not as plain html using an Html text helper like Html.TextArea?

[AllowHtml]
public string Description { get; set; }

I have tried setting my View for the Description like this instead of DisplayFor, but it still doesn't work:

<dd>
    @Model.Description
    @*@Html.DisplayFor(model => model.Description)*@
</dd>

Upvotes: 2

Views: 4348

Answers (2)

Erik Philips
Erik Philips

Reputation: 54628

Although you can use

@Html.Raw(Model.Description)

I personally don't like it because you can't do anything with it in terms of Display/Editor templates. If you plan on using any templates, then I would highly recommend using MvcHtmlString as the type for your model:

public MvcHtmlString Description { get; set; }

Now you can:

<dd>
   @Html.DisplayFor(model => model.Description)
</dd>

And if needed override the template:

[MvcHtmlString.cshtml]

<dd>
   @model
</dd>

Upvotes: 2

Mahedi Sabuj
Mahedi Sabuj

Reputation: 2944

Try @Html.Raw(Model.Description) instead of @Model.Description

Upvotes: 7

Related Questions