Comment puis-je remplacer le modèle @ Html.LabelFor?

J’ai un formulaire de champ simple

@Html.LabelFor(model => model.Register.UserName) @Html.TextBoxFor(model => model.Register.UserName)

et cela se traduit par:

 

mais je veux que le code LabelFor ajoute un intérieur pour que je puisse finir par avoir:

  

Comment puis-je faire ceci?

Tous les exemples utilisent EditorTemplates mais c’est un LabelFor .

Vous feriez cela en créant votre propre aide HTML.

http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

Vous pouvez afficher le code pour LabelFor <> en téléchargeant le source pour ASP.Net MVC et le modifier en tant qu’assistant personnalisé.


Réponse ajoutée par balexandre

 public static class LabelExtensions { public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, object htmlAtsortingbutes) { return LabelFor(html, expression, new RouteValueDictionary(htmlAtsortingbutes)); } public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, IDictionary htmlAtsortingbutes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); ssortingng htmlFieldName = ExpressionHelper.GetExpressionText(expression); ssortingng labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (Ssortingng.IsNullOrEmpty(labelText)) { return MvcHtmlSsortingng.Empty; } TagBuilder tag = new TagBuilder("label"); tag.MergeAtsortingbutes(htmlAtsortingbutes); tag.Atsortingbutes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); TagBuilder span = new TagBuilder("span"); span.SetInnerText(labelText); // assign  to  

LabelFor est une méthode d’extension (statique) et ne peut donc pas être remplacée. Vous devez créer votre propre méthode d’extension d’aide HTML pour obtenir ce dont vous avez besoin.

J’ai développé la réponse de balealexandre et ajouté la possibilité de spécifier HTML pour inclure à la fois avant et après le texte de votre étiquette. J’ai ajouté un tas de surcharges de méthodes et de commentaires. J’espère que cela aide les gens!

Informations également interceptées ici: Html à l’intérieur de l’étiquette utilisant l’assistant HTML

 namespace System.Web.Mvc.Html { public static class LabelExtensions { /// Creates a Label with custom Html before the label text. Only starting Html is provided. /// Html to preempt the label text. /// MVC Html for the Label public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, Func startHtml) { return LabelFor(html, expression, startHtml, null, new RouteValueDictionary("new {}")); } /// Creates a Label with custom Html before the label text. Starting Html and a single Html atsortingbute is provided. /// Html to preempt the label text. /// A single Html atsortingbute to include. /// MVC Html for the Label public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, object htmlAtsortingbutes) { return LabelFor(html, expression, startHtml, null, new RouteValueDictionary(htmlAtsortingbutes)); } /// Creates a Label with custom Html before the label text. Starting Html and a collection of Html atsortingbutes are provided. /// Html to preempt the label text. /// A collection of Html atsortingbutes to include. /// MVC Html for the Label public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, IDictionary htmlAtsortingbutes) { return LabelFor(html, expression, startHtml, null, htmlAtsortingbutes); } /// Creates a Label with custom Html before and after the label text. Starting Html and ending Html are provided. /// Html to preempt the label text. /// Html to follow the label text. /// MVC Html for the Label public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml) { return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary("new {}")); } /// Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a single Html atsortingbute are provided. /// Html to preempt the label text. /// Html to follow the label text. /// A single Html atsortingbute to include. /// MVC Html for the Label public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml, object htmlAtsortingbutes) { return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary(htmlAtsortingbutes)); } /// Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a collection of Html atsortingbutes are provided. /// Html to preempt the label text. /// Html to follow the label text. /// A collection of Html atsortingbutes to include. /// MVC Html for the Label public static MvcHtmlSsortingng LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml, IDictionary htmlAtsortingbutes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); ssortingng htmlFieldName = ExpressionHelper.GetExpressionText(expression); //Use the DisplayName or PropertyName for the metadata if available. Otherwise default to the htmlFieldName provided by the user. ssortingng labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (Ssortingng.IsNullOrEmpty(labelText)) { return MvcHtmlSsortingng.Empty; } //Create the new label. TagBuilder tag = new TagBuilder("label"); //Add the specified Html atsortingbutes tag.MergeAtsortingbutes(htmlAtsortingbutes); //Specify what property the label is tied to. tag.Atsortingbutes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); //Run through the various iterations of null starting or ending Html text. if (startHtml == null && endHtml == null) tag.InnerHtml = labelText; else if (startHtml != null && endHtml == null) tag.InnerHtml = ssortingng.Format("{0}{1}", startHtml(null).ToHtmlSsortingng(), labelText); else if (startHtml == null && endHtml != null) tag.InnerHtml = ssortingng.Format("{0}{1}", labelText, endHtml(null).ToHtmlSsortingng()); else tag.InnerHtml = ssortingng.Format("{0}{1}{2}", startHtml(null).ToHtmlSsortingng(), labelText, endHtml(null).ToHtmlSsortingng()); return MvcHtmlSsortingng.Create(tag.ToSsortingng()); } } }