Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an <a href="http://cru.caes.ucdavis.edu/blog/posts/its-magic/extend-mvc-labelfor-method" rel="noreferrer">blog post</a> that describes how to do this.</p> <p>To give you a small example modified from the site above (note - I have not compiled/tested this):</p> <pre><code>namespace HelpRequest.Controllers.Helpers { public static class LabelExtensions { public static MvcHtmlString Label(this HtmlHelper html, string expression, string id = "", bool generatedId = false) { return LabelHelper(html, ModelMetadata.FromStringExpression(expression, html.ViewData), expression, id, generatedId); } [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] public static MvcHtmlString LabelFor&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; html, Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression, string id = "", bool generatedId = false) { return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), id, generatedId); } internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string id, bool generatedId) { string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (String.IsNullOrEmpty(labelText)) { return MvcHtmlString.Empty; } var sb = new StringBuilder(); sb.Append(labelText); if (metadata.IsRequired) sb.Append("*"); var tag = new TagBuilder("label"); if (!string.IsNullOrWhiteSpace(id)) { tag.Attributes.Add("id", id); } else if (generatedId) { tag.Attributes.Add("id", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName) + "_Label"); } tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); tag.SetInnerText(sb.ToString()); return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload