Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why can't you create these with an @Html. If you write your own extension method (see below) that should do it, no?</p> <pre><code> public static class HtmlHelperExtensions { public static MvcHtmlString MyCheckBoxFor&lt;TModel&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, bool&gt;&gt; expression, object htmlLabelAttributes = null, object htmlCheckBoxAttributes = null) { var checkbox = htmlHelper.CheckBoxFor(expression, htmlCheckBoxAttributes); var labelTag = new TagBuilder(&quot;label&quot;); labelTag.AddCssClass(&quot;checkbox&quot;); labelTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlLabelAttributes)); labelTag.InnerHtml = checkbox.ToString(); return new MvcHtmlString(labelTag.ToString()); } } </code></pre> <h2>EDIT:</h2> <p>How about this revised version. This replicates exactly what the label does.</p> <pre><code> public static class HtmlHelperExtensions { public static MvcHtmlString MyCheckBoxFor&lt;TModel&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, bool&gt;&gt; expression, object htmlLabelAttributes = null, object htmlCheckBoxAttributes = null) { var checkbox = htmlHelper.CheckBoxFor(expression, htmlCheckBoxAttributes); var labelTag = new TagBuilder(&quot;label&quot;); var checkboxName = ExpressionHelper.GetExpressionText(expression); labelTag.AddCssClass(&quot;checkbox&quot;); labelTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlLabelAttributes)); labelTag.InnerHtml = checkbox.ToString() + LabelHelper(ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData), checkboxName); return new MvcHtmlString(labelTag.ToString()); } private static MvcHtmlString LabelHelper(ModelMetadata metadata, string fieldName) { string labelText; var displayName = metadata.DisplayName; if (displayName == null) { var propertyName = metadata.PropertyName; labelText = propertyName ?? fieldName.Split(new[] { &#39;.&#39; }).Last(); } else { labelText = displayName; } if (string.IsNullOrEmpty(labelText)) { return MvcHtmlString.Empty; } return new MvcHtmlString(labelText); } } </code></pre> <p>I should note that with MVC 4 there is a DisplayNameFor Helper, so that whole label business could be simplified a bit.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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