the Quantity of items")] public bool IsLimite...">

How to insert line break using LabelFor in MVC

In my model:

[Display(Name = "Check to enter <break> the Quantity of items")] public bool IsLimitedQuantity { get; set; } 

and i use

 @Html.LabelFor(shop => shop.IsLimitedQuantity) 

in my opinion.

Please suggest how I can fix this, because the label simply shows the <break> as it is, instead of breaking to a new line.

+2
asp.net-mvc line-breaks
source share
2 answers

You can write a LabelFor custom helper that does not encode HTML text in the same way as the standard LabelFor helper:

 public static class LabelExtensions { public static IHtmlString UnencodedLabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression) { var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); var htmlFieldName = ExpressionHelper.GetExpressionText(expression); var text = (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>())); if (string.IsNullOrEmpty(text)) { return MvcHtmlString.Empty; } var tagBuilder = new TagBuilder("label"); tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName))); tagBuilder.InnerHtml = text; return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal)); } } 

and then use this custom helper in the view:

 @Html.UnencodedLabelFor(x => x.IsLimitedQuantity) 

Now HTML tags in the display name will be displayed without encoding:

 [Display(Name = "Check to enter <br/> the Quantity of items")] public bool IsLimitedQuantity { get; set; } 
+7
source share

Using HTML decoding can help. MVC encodes all values ​​for security reasons.

How to display HTML stored in a database from an ASP.NET MVC view?

0
source share

All Articles