Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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