0

Currently I'm busy with a project for school. I was optimizing my FAQ and I want to parse HTML in my TEXT column for my answer. Let me explain it better.

I have my database model, which is this:

public class FAQModel
{
    public int Id { get; set; }
    [Required]
    public string Question { get; set; }
    [Required]
    public string Answer { get; set; }
}

And in my view:

<div><p>@item.Answer</p></div>

But now HTML (which is in my Answer column) will be like text, so it won't be parsed. Like this is my result:

Ga naar <a href="../EmailSub/">deze pagina</a>

But I want it to be a link which I can click on. Is there any way I can do this?

3 Answers 3

2

Not sure what the question is, but if you have HTML string in the Answer column in your database, like: "Ga naar <a href="../EmailSub/">deze pagina</a>"

Then you should use:

<div><p>@Html.Raw(item.Answer)</p></div>

But I only recommend to use this approach if the answer field in the database is olny filled by administrators, not users, because this is a potential vulnerability called cross-site scripting (XSS)!

Sign up to request clarification or add additional context in comments.

Comments

1

Use Html.Raw method will convert the string stored in the db to the DOM elements structure:

@Html.Raw(item.Answer)

Comments

1

I think that you can get your goal using Html.Raw. Replace

<div><p>@item.Answer</p></div>

with

<div><p>@Html.Raw(item.Answer)</p></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.