Last active
November 17, 2015 12:35
-
-
Save komainu85/d06240bf46d1f5e19b8b to your computer and use it in GitHub Desktop.
Razor Helpers Html Attributes Fluent API
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 MikeRobbins.SitecoreUtilities.RazorHelpers | |
{ | |
public class HtmlAttributes | |
{ | |
private NameValueCollection _nameValueCollection = new NameValueCollection(); | |
public HtmlAttributes CssClass(string cssClass) | |
{ | |
_nameValueCollection.Add("class", cssClass); | |
return this; | |
} | |
public HtmlAttributes Image(int height, int width) | |
{ | |
_nameValueCollection.Add("height", height.ToString()); | |
_nameValueCollection.Add("width", width.ToString()); | |
return this; | |
} | |
public HtmlAttributes Attribute(string attributeName, string attributeValue) | |
{ | |
_nameValueCollection.Add(attributeName, attributeValue); | |
return this; | |
} | |
public NameValueCollection RenderEditable() | |
{ | |
return _nameValueCollection; | |
} | |
public Dictionary<string, object> Render() | |
{ | |
return ToDictionary(_nameValueCollection); | |
} | |
public static Dictionary<string, object> ToDictionary(NameValueCollection nvc) | |
{ | |
return nvc.AllKeys.ToDictionary<string, string, object>(k => k, k => nvc[k]); | |
} | |
} | |
} |
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
@using MikeRobbins.SitecoreUtilities.RazorHelpers | |
@Html.TextBox("StandardRazor", new {@class= "TextBox", data_url = "SomeURL" }) | |
@Html.TextBox("HelperClass", new MVCHtmlAttributes().CssClass("TextBox").Attribute("data-url", "SomeURL").Render()) | |
Glass | |
@Editable(x => x.Link, new HtmlAttributes().CssClass("Link").Attribute("data-url", "SomeURL").RenderEditable()) | |
@RenderImage(x=> x.Image, new HtmlAttributes().CssClass("Image").Attribute("data-url", "SomeURL").Render()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment