How to make Html.DisplayFor displaying line breaks?

Confusing newbie question:

In my model, there is a string field containing line breaks.

 @Html.DisplayFor(x => x.MultiLineText) 

does not display line breaks.

Obviously, I could play in the model and create another field that replaces \n with <br/> , but it looks like kludgy. What is the way the tutorial does this work?

+44
asp.net-mvc-3 razor
Jan 27 2018-12-12T00:
source share
7 answers

HtmlHelper extension method for displaying string values ​​with line breaks:

 public static MvcHtmlString DisplayWithBreaksFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); var model = html.Encode(metadata.Model).Replace("\r\n", "<br />\r\n"); if (String.IsNullOrEmpty(model)) return MvcHtmlString.Empty; return MvcHtmlString.Create(model); } 

And then you can use the following syntax:

 @Html.DisplayWithBreaksFor(m => m.MultiLineField) 
+68
Apr 24 '12 at 1:40
source share

I recommend formatting the output with css instead of using a processor that uses server-side processors like .replace,

just add this style property to render multi-line texts:

 .multiline { white-space: pre-wrap; } 

then

 <div class="multiline"> my multiline text </div> 

newlines will display as br elements, test it here http://refork.com/xaf4

+43
Jun 24 '13 at 10:34 on
source share

In your opinion, you can try something like

 @Html.Raw(Html.Encode(Model.MultiLineText).Replace("\n", "<br />")) 
+42
Jan 27
source share

The display template is probably the best solution, but there is another easy way to use the html helper if you know that you are just showing a string, for example:

 namespace Shaul.Web.Helpers { public static class HtmlHelpers { public static IHtmlString ReplaceBreaks(this HtmlHelper helper, string str) { return MvcHtmlString.Create(str.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).Aggregate((a, b) => a + "<br />" + b)); } } } 

And then you will use it like:

 @using Shaul.Web.Helpers @Html.ReplaceBreaks(Model.MultiLineText) 
+4
Jan 27 '12 at 10:17
source share

You create a display template for your data. Here is a post detailing how to do this. How to create an MVC Razor template for DisplayFor ()

In this template, you are actually translating newlines into
and any other work that needs to be done for the presentation.

+3
Jan 27 2018-12-12T00:
source share

Try using @Html.Raw("<p>" + Html.LabelFor(x => x.Name) + "</p>")

+2
Jan 27 '12 at 8:45
source share

Here is another variation of the extension method.

  public static IHtmlString DisplayFormattedFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, string>> expression) { string value = Convert.ToString(ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model); if (string.IsNullOrWhiteSpace(value)) { return MvcHtmlString.Empty; } value = string.Join("<br/>", value.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(HttpUtility.HtmlEncode)); return new HtmlString(value); } 
0
Sep 22 '15 at 15:22
source share



All Articles