Why can't you create these with an @Html. If you write your own extension method (see below) that should do it, no?
public static class HtmlHelperExtensions
{
public static MvcHtmlString MyCheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, object htmlLabelAttributes = null, object htmlCheckBoxAttributes = null)
{
var checkbox = htmlHelper.CheckBoxFor(expression, htmlCheckBoxAttributes);
var labelTag = new TagBuilder("label");
labelTag.AddCssClass("checkbox");
labelTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlLabelAttributes));
labelTag.InnerHtml = checkbox.ToString();
return new MvcHtmlString(labelTag.ToString());
}
}
EDIT:
How about this revised version. This replicates exactly what the label does.
public static class HtmlHelperExtensions
{
public static MvcHtmlString MyCheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, object htmlLabelAttributes = null, object htmlCheckBoxAttributes = null)
{
var checkbox = htmlHelper.CheckBoxFor(expression, htmlCheckBoxAttributes);
var labelTag = new TagBuilder("label");
var checkboxName = ExpressionHelper.GetExpressionText(expression);
labelTag.AddCssClass("checkbox");
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[] { '.' }).Last();
}
else
{
labelText = displayName;
}
if (string.IsNullOrEmpty(labelText))
{
return MvcHtmlString.Empty;
}
return new MvcHtmlString(labelText);
}
}
I should note that with MVC 4 there is a DisplayNameFor Helper, so that whole label business could be simplified a bit.