Skip to content

Instantly share code, notes, and snippets.

@komainu85
Last active November 17, 2015 12:35
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 komainu85/d06240bf46d1f5e19b8b to your computer and use it in GitHub Desktop.
Save komainu85/d06240bf46d1f5e19b8b to your computer and use it in GitHub Desktop.
Razor Helpers Html Attributes Fluent API
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]);
}
}
}
@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