Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created January 4, 2013 12:09
Show Gist options
  • Select an option

  • Save ChrisMcKee/4452168 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/4452168 to your computer and use it in GitHub Desktop.
usage i.e. String Editor Template... usage ala first comment
using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
public static class ControlGroupExtensions
{
public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html,
Expression<Func<T, object>> modelProperty)
{
var controlGroupWrapper = new TagBuilder("div");
controlGroupWrapper.AddCssClass("control-group");
var partialFieldName = ExpressionHelper.GetExpressionText(modelProperty);
var fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(partialFieldName);
if (!html.ViewData.ModelState.IsValidField(fullHtmlFieldName))
{
controlGroupWrapper.AddCssClass("error");
}
var openingTag = controlGroupWrapper.ToString(TagRenderMode.StartTag);
return MvcHtmlString.Create(openingTag);
}
public static IHtmlString EndControlGroup(this HtmlHelper html)
{
return MvcHtmlString.Create("</div>");
}
}
@ChrisMcKee
Copy link
Author

@inherits WebViewPage<string>

@Html.BeginControlGroupFor(x => x)
    @Html.LabelFor(x => x, new { @class = "control-label" })
    <div class="controls">
        @Html.TextBoxFor(x => x, ViewData["htmlAttributes"])
        @Html.ValidationMessageFor(x => x)
    </div>
@Html.EndControlGroup()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment