开发者

mvc3 multiline text in a cell

开发者 https://www.devze.com 2023-04-12 08:07 出处:网络
I have text with \"\\n\" in my database. How can i display the linebreaks in the index view (<br />)

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>
            @it开发者_如何转开发em.tel1
        </td>
        <td>
            @item.infos
        </td>
    </tr>

}


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 />")
 )


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消