Created
October 29, 2013 22:58
-
-
Save paulblamire/7224199 to your computer and use it in GitHub Desktop.
Helper to reduce the amount of Bootstrap 3.0 chrome that needs to be manual written around standard HtmlHelper extensions
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.WebPages; | |
using System.Linq.Expressions; | |
using System.Web.Mvc.Html; | |
using System.Text; | |
namespace System.Web.Mvc | |
{ | |
public static class BootstrapHelper | |
{ | |
public static IDisposable BeginFields(this HtmlHelper helper) | |
{ | |
helper.ViewContext.Writer.Write(helper.AntiForgeryToken()); | |
helper.ViewContext.Writer.Write("<div class=\"form-horizontal\">"); | |
return new EndFields(helper); | |
} | |
class EndFields : IDisposable | |
{ | |
private HtmlHelper helper; | |
public EndFields(HtmlHelper helper) | |
{ | |
this.helper = helper; | |
} | |
public void Dispose() | |
{ | |
this.helper.ViewContext.Writer.Write("</div>"); | |
} | |
} | |
public static MvcHtmlString Field<T, V>(this HtmlHelper<T> helper, Expression<Func<T, V>> member, Func<Expression<Func<T, V>>, HelperResult> template) | |
{ | |
return Concat( | |
StartHtml("div", b => b.AddCssClass("form-group")), | |
helper.LabelFor(member, new { @class = "control-label col-md-2" }), | |
StartHtml("div", b => b.AddCssClass("col-md-10")), | |
MvcHtmlString.Create(template(member).ToHtmlString()), | |
EndHtml("div"), | |
EndHtml("div") | |
); | |
} | |
public static MvcHtmlString SubmitButton(this HtmlHelper helper, string value) | |
{ | |
return Concat( | |
StartHtml("div", b => b.AddCssClass("form-group")), | |
StartHtml("div", b => b.AddCssClass("col-md-offset-2 col-md-10")), | |
BuildHtml("input", b => { b.AddCssClass("btn btn-default"); b.Attributes["type"] = "submit" ; b.Attributes["value"] = value; }), | |
EndHtml("div"), | |
EndHtml("div") | |
); | |
} | |
private static MvcHtmlString StartHtml(string element, Action<TagBuilder> builder) | |
{ | |
return BuildHtml(element, builder, TagRenderMode.StartTag); | |
} | |
private static MvcHtmlString EndHtml(string element) | |
{ | |
return BuildHtml(element, null, TagRenderMode.EndTag); | |
} | |
private static MvcHtmlString BuildHtml(string element, Action<TagBuilder> buildUp, TagRenderMode mode = TagRenderMode.Normal) | |
{ | |
TagBuilder tagBuilder = new TagBuilder(element); | |
if (buildUp != null) buildUp(tagBuilder); | |
return MvcHtmlString.Create(tagBuilder.ToString(mode)); | |
} | |
private static MvcHtmlString Concat(params MvcHtmlString[] items) | |
{ | |
var sb = new StringBuilder(); | |
foreach (var item in items.Where(i => i != null)) | |
sb.Append(item.ToHtmlString()); | |
return MvcHtmlString.Create(sb.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment