Diego Ferrero
Diego Ferrero

Reputation: 41

ASP.NET MVC table row including link on text from model

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?

Upvotes: 0

Views: 608

Answers (1)

mxmissile
mxmissile

Reputation: 11673

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

@Html.Raw(Html.DisplayFor(modelItem => item.message))

Upvotes: 1

Related Questions