Can I change the way LabelFor render in MVC?

I would like to change the way LabelFor is rendered. Can I do this with a DisplayTemplate?

Label Create a label for the label, and I would like to add a ":" at the end of the label.

Thank you!

Alex

+4
source share
5 answers

Here is the HTML helper that will do this:

public static class LabelExtensions { [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] public static MvcHtmlString SmartLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression)); } internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName) { string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (String.IsNullOrEmpty(labelText)) { return MvcHtmlString.Empty; } // uncomment if want * for required field //if (metadata.IsRequired) labelText = labelText + " *"; labelText = labelText + ":"; TagBuilder tag = new TagBuilder("label"); tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); tag.SetInnerText(labelText); return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); } } 

To use it:

 <%:Html.SmartLabelFor(m => m.FirstName)%> 

It will display:

 <label for="FirstName">First Name:</label> 

Or if you uncomment the required field *

 <label for="FirstName">First Name *:</label> 
+7
source

Just write a regular <label> element in plain HTML:

 <label>My Label:</label> 

If you want to display the for="" attribute and accurately visualize the name of the control, use this extension method:

 using System; using System.Linq.Expressions; using System.Web.Mvc; namespace MvcLibrary.Extensions { public static class HtmlExtensions { public static MvcHtmlString FieldIdFor<TModel, TValue>( this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string inputFieldId = html.ViewContext.ViewData. TemplateInfo.GetFullHtmlFieldId(htmlFieldName); return MvcHtmlString.Create(inputFieldId); } } } 

Then you can use in your view like this:

 <label for="<%= Html.FieldIdFor(m => m.EmailAddress) %>">E-mail address:</label> <%= Html.TextBoxFor(m => m.EmailAddress) %> 

Other posts cover different approaches, they are all equally valid, and you are engaged in personal preference. I personally prefer to write <label> as plain HTML, as it gives developers more flexibility when changing markup, adding extra attributes like CSS classes, etc. I also believe that the text of the label is a matter of consideration and should not be framed in the ViewModel class, but this is only my personal opinion / preference, I know that some people here will not agree with me and this fine :-)

+2
source

You can create String.ascx in the DisplayTemplates folder and provide your own implementation. See the section Overriding Templates in the next article.

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

+1
source

You can do this using MVC 2 (if possible) if you pass the custom ViewModel to the view.

 using System.ComponentModel; public class PersonViewModel { public PersonViewModel(string name) { this.Name = name; } [DisplayName(".Display Anything You Like Here.")] public string Name { get; set; } 
+1
source

I think the best approach is to write your own helper method that will do what you like. You can overload an existing method or just create a new method.

0
source

Source: https://habr.com/ru/post/1314323/


All Articles