Skip to content

Instantly share code, notes, and snippets.

@tboby
Last active January 8, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tboby/8b2587a94a6e494d955e to your computer and use it in GitHub Desktop.
Save tboby/8b2587a94a6e494d955e to your computer and use it in GitHub Desktop.
Helper Example
@functions
{
public static HelperResult ValidatedElementFor<TModel, TProperty>(HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expr, object editorHtmlAttributes = null)
{
var defaultHtmlAttributes = new { @class = "form-control" };
var combinedHtmlAttributes = html.MergeHtmlAttributes(defaultHtmlAttributes, editorHtmlAttributes);
return ValidatedElement(html.ValidationErrorFor(expr, " has-error", " has-success"), html.LabelFor(expr, htmlAttributes: new { @class = "control-label col-sm-3" }), html.EditorFor(expr, new { htmlAttributes= combinedHtmlAttributes}), html.ValidationMessageFor(expr, "", new { @class = "text-danger" }));
}
}
@helper ValidatedElement(MvcHtmlString error, MvcHtmlString label, MvcHtmlString editor, MvcHtmlString message)
{
<div class="form-group has-feedback@(error)">
@label
<div class="col-sm-9">
@editor
<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
<span class="glyphicon glyphicon-info-sign form-control-feedback" aria-hidden="true"></span>
<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>
@message
</div>
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment