In my case I was trying to create an HTML5 number input editor template that could receive additional attributes. A neater approach would be to write your own HTML Helper, but since I already had my .ascx template, I went with this approach:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input id="<%= Regex.Replace(ViewData.TemplateInfo.GetFullHtmlFieldId(""), @"[\[\]]", "_") %>" name="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>" type="number" value="<%= ViewData.TemplateInfo.FormattedModelValue %>"
<% if (ViewData["attributes"] != null)
{
Dictionary<string, string> attributes = (Dictionary<string, string>)ViewData["attributes"];
foreach (string attributeName in attributes.Keys){%>
<%= String.Format(" {0}=\"{1}\"", attributeName, attributes[attributeName])%>
<% }
} %> />
This ugly bit creates a number type input and looks for a ViewData Dictionary with the key "attributes". It will iterate through the dictionary adding its key/value pairs as attributes. The Regex in the ID attribute is unrelated and is there because when used in a collection, GetFullHtmlFieldId()
returns an id containing square brackets []
which it would normally escape as underscores.
This template is then called like this:
Html.EditorFor(m => m.Quantity, "NumberField", new { attributes = new Dictionary<string, string>() { { "class", "txtQuantity" } } }
Verbose, but it works. You could probably use reflection in the template to use property names as attribute names instead of using a dictionary.