amfilipiak amfilipiak - 5 months ago 31
C# Question

Rendering HTML stored in database as nvarchar

I have been trying to figure this out for a few days. I was able to update my app to allow the user (me) to input HTML code into a text field and update that info to the database. However, the problem is, when I then try to display that information, the HTML is not rendered but is showing as raw HTML.

I have searched this site for an answer to this but have been unable to.

I need the HTML to be rendered so that my CSS and JavaScript can do their thing and format the elements.

If anyone can point me in the right direction on this it would be great as it would be impossible for me to hard code the HTML into the view directly.

Example would be

<span id="{1c}}></span>
javascript/css will fill in the blank. However, the HTML is displayed just as it shows and not as blank space. Again, this is just an example.

Adding Code:

From Card.cs

public string CardText { get; set; }

Data Input into text field and saved to database.

<span id="{t}">Tap</span>Prodigal Sorcerer Deals 1 damage to target creature or player.

Call in view:


Displayed on page:

<span id={t}">Tap</span>Prodigal sorcerer Deals 1 damage to target creature or player.

Inspect Page source displays:

<p>&lt;span id=&quot;{t}&quot;&gt;Tap&lt;/span&gt;Prodigal Sorcerer Deals 1 damage to target creature or player.</p>

I have tried to update the view with the following however the same response is received


and also without the quotes


The both return what is stored in the database but with the html as string data and not rendered html.


Use this:


Think of it like this: in MVC the @ symbol will automatically encode what you put behind it, unless you 'wrap' it first by using Html.Raw.