asp.net mvc convert \n new line to html breaks

前端 未结 11 624
逝去的感伤
逝去的感伤 2020-12-14 15:10

I have a textarea in mvc. When data is entered into that and I\'m displaying it back to the user, how do I show the line breaks?

I display like this:

11条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 15:57

    For a slightly different (and, IMHO, better and safer) way of solving the problem, see this answer to a similar question.

    Basically, instead of going through the trouble of converting all new line characters to
    elements, it is as simple as applying the following CSS to the element where you are showing the inputted text:

    white-space: pre-line
    

提交回复
热议问题