Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I found a little nicer solution to it. EditorFor takes in additionalViewData as a parameter. If you give it a parameter named "htmlAttributes" with the attributes, then we can do interesting things with it:</p> <pre><code>@Html.EditorFor(model =&gt; model.EmailAddress, new { htmlAttributes = new { @class = "span4", maxlength = 128, required = true, placeholder = "Email Address", title = "A valid email address is required (i.e. user@domain.com)" } }) </code></pre> <p>In the template (in this case, EmailAddress.cshtml) you can then provide a few default attributes:</p> <pre><code>@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, Html.MergeHtmlAttributes(new { type = "email" })) </code></pre> <p>The magic comes together through this helper method:</p> <pre><code>public static IDictionary&lt;string, object&gt; MergeHtmlAttributes&lt;TModel&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, object htmlAttributes) { var attributes = htmlHelper.ViewData.ContainsKey("htmlAttributes") ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlHelper.ViewData["htmlAttributes"]) : new RouteValueDictionary(); if (htmlAttributes != null) { foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes)) { var key = property.Name.Replace('_', '-'); if (!attributes.ContainsKey(key)) { attributes.Add(key, property.GetValue(htmlAttributes)); } } } return attributes; } </code></pre> <p>Of course you could modify it to render the attributes as well if you are doing raw HTML:</p> <pre><code>public static MvcHtmlString RenderHtmlAttributes&lt;TModel&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, object htmlAttributes) { var attributes = htmlHelper.ViewData.ContainsKey("htmlAttributes") ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlHelper.ViewData["htmlAttributes"]) : new RouteValueDictionary(); if (htmlAttributes != null) { foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes)) { var key = property.Name.Replace('_', '-'); if (!attributes.ContainsKey(key)) { attributes.Add(key, property.GetValue(htmlAttributes)); } } } return MvcHtmlString.Create(String.Join(" ", attributes.Keys.Select(key =&gt; String.Format("{0}=\"{1}\"", key, htmlHelper.Encode(attributes[key]))))); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload