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:
You should always encode user entered text when displaying it back in the view to ensure that it is safe.
You could do as Cybernate suggested or could add it to a HtmlHelper extension method
public static string EncodedMultiLineText(this HtmlHelper helper, string text) {
if (String.IsNullOrEmpty(text)) {
return String.Empty;
}
return Regex.Replace(helper.Encode(text), Environment.NewLine, "
")
}
So that it can be easily reused in you view
<%= Html.EncodedMultiLineText(Model.Description) %>