Display multiline text in razor
When user wants post data, we are using textarea for input. This data is stored into database and we are retrieving it using razor @Html.DisplayFor().
The Data for textarea is like this (inputed by user)
Shall I compare thee to a summer’s day? Thou art more lovely and more temperate:Rough winds do shake thedarling buds of May,And summer’s lease hath all too short a date: Sometime too hot the eye of heaven shines,And often is his gold complexion dimm’d;
How can we format display text?
Solution:
The Data for textarea is like this (inputed by user)
Shall I compare thee to a summer's day? Thou art more lovely and more temperate: Rough winds do shake the darling buds of May, And summer's lease hath all too short a date: Sometime too hot the eye of heaven shines, And often is his gold complexion dimm'd;and when we retrieve it using this,
<em>@Html.DisplayFor(model => item.inputvalue)</em>but it is displayed as unformated text like below,
Shall I compare thee to a summer’s day? Thou art more lovely and more temperate:Rough winds do shake thedarling buds of May,And summer’s lease hath all too short a date: Sometime too hot the eye of heaven shines,And often is his gold complexion dimm’d;
How can we format display text?
Solution:
@Html.Raw(Model.Inputvalue.Replace(Environment.NewLine, "<br/>"))
If you don't know if the text will use the same line endings as your server's environment, you might want to use @Html.Raw(Regex.Replace(Model.Inputvalue, @"\r?\n", "
ReplyDelete"))