Created
September 4, 2013 23:32
-
-
Save SteelHammerhands/6444221 to your computer and use it in GitHub Desktop.
Custom MVC Html extension. Wraps a LabelFor, EditorFor, and ValidationMessageFor for a property in a div. Saves a considerable amount of typing in Views. Also checks if the property has the Required attribute and adds a class to the label. Can pass in Html attributes which get applied to the containing div. This gives you hooks for styling and l…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace System.Web.Mvc | |
{ | |
public static class MyEditorForExtensions | |
{ | |
public static MvcHtmlString MyEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) | |
{ | |
return MyEditorForInternal(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(null)); | |
} | |
public static MvcHtmlString MyEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) | |
{ | |
return MyEditorForInternal(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); | |
} | |
private static MvcHtmlString MyEditorForInternal<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) | |
{ | |
TagBuilder builder = new TagBuilder("div"); | |
builder.MergeAttributes(htmlAttributes); | |
string labelHtml; | |
var test = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); | |
if (test.IsRequired) | |
{ | |
labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression, new { @class = "required" }).ToHtmlString(); | |
} | |
else | |
{ | |
labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString(); | |
} | |
string editorHtml = Html.EditorExtensions.EditorFor(htmlHelper, expression).ToHtmlString(); | |
string validationHtml = Html.ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString(); | |
builder.InnerHtml = labelHtml + editorHtml + validationHtml; | |
return new MvcHtmlString(builder.ToString(TagRenderMode.Normal)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fieldset {border:1px #ccc dotted; padding:0% 1%; margin:10px -10px;} | |
legend {font-weight:bold; font-size:123%; padding:0px 3px; margin-left:-3px; background-color:White;} | |
form div{float:left; margin:2% 3%;} | |
form div.oneSixth {width:10.4%;} | |
form div.oneThird {width:26.7%;} | |
form div.twoThirds {width:59.5%;} | |
form div.half {width:43%;} | |
form div.oneFourth{width:18.5%;} | |
label {display:block; margin-bottom:1px; width:auto; text-align:left;} | |
.required:after {content:url(images/icon_required.gif);} | |
form div.inline {margin:1% 3%;} | |
form div.inline input {width:60%; border:1px solid #ccc; text-align:left;} | |
form div.inline label {display:inline-block; width:30%; text-align:right; padding-right:12px;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment