Matthias Engeler
Matthias Engeler

Reputation: 17

mvc3 multiline text in a cell

I have text with "\n" in my database. How can i display the linebreaks in the index view (<br />)

Simple "replace" is not working, it gets encoded:

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink(item.name, "Details", new { id = item.personId })
        </td>
        <td>
            @item.adresse.Replace("\n", "<br />")
        </td>
        <td>
            @item.tel1
        </td>
        <td>
            @item.infos
        </td>
    </tr>

}

Upvotes: 0

Views: 551

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

If the data is originally not entered by a user, or you have already sanitized it

@Html.Raw(item.adresse.Replace("\n", "<br />"))

Otherwise Cshift3iLike's answer is good for unsanitized input.

Upvotes: 0

Nikita Ignatov
Nikita Ignatov

Reputation: 7163

When you are doing somehting like this, it is very important to encode the text, that is going to be shown, in order to avoid harmful javascript code

 @MvcHtmlString.Create(
          Html.Encode(item.adresse).Replace(Enviornment.NewLine, "<br />")
 )

Upvotes: 1

Related Questions