0

I'm trying to display a table from a model. But the problem is that my model class has a string that already includes an <a> tag.

My view is:

@model MyWeb.Models.Incidente
@{
    Layout = null;
    ViewBag.Title = "Communication for " + Model.num.ToString();
}

<h2>@ViewBag.Title</h2>

<table class="table">
    <tr>
        <th> User </th>
        <th> Date </th>
        <th> Time </th>
        <th> Message </th>
    </tr>

    @foreach (MyWeb.Models.Comunication item in Model.GetComunications())
    {
        <tr>
            <td> @Html.DisplayFor(modelItem => item.user) </td>
            <td> @Html.DisplayFor(modelItem => item.date) </td>
            <td> @Html.DisplayFor(modelItem => item.time) </td>
            <td> @Html.DisplayFor(modelItem => item.message) </td>
        </tr>
    }

</table>

The problem is that one item in GetComunications() has a message that include a link to Google:

You can Google it, just press <a href="http://www.google.com">Here!</a>. Thank you!

So when the table is displayed the message shows the tag instead of creating the link in "Here!".

How can I solve this problem, and show a message that already has a link on the string?

Mukyuu
  • 6,436
  • 8
  • 40
  • 59
  • Does this answer your question? [Display string as html in asp.net mvc view](https://stackoverflow.com/questions/19980657/display-string-as-html-in-asp-net-mvc-view) – devlin carnate Feb 06 '20 at 16:36

1 Answers1

1

If you are sure about the sanitation of your output, you can use Html.Raw:

@Html.Raw(Html.DisplayFor(modelItem => item.message))
mxmissile
  • 11,464
  • 3
  • 53
  • 79
  • Thanks for your answer, but @HTML.Raw is not working... If I inspect my page the code appears " You can Google it, just press Here!. Thank you " The string is quoted – Diego Ferrero Feb 06 '20 at 16:55