views:

70

answers:

3
<%= Html.EditorFor(product => product.Name) %>

I need the generated output to have autocomplete="off" attribute set.

What I'm missing?

Edit: I'm looking an extension method for EditorFor that accepts key/value dictionary for attributes, so I can call it like this: <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" } ) %>

Here it is done for LabelFor, but it is needed to be adjusted for EditorFor

    public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) {
        return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes));
    }
    public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
        string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
        string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
        if (String.IsNullOrEmpty(labelText))
        {
            return MvcHtmlString.Empty;
        }

        TagBuilder tag = new TagBuilder("label");
        tag.MergeAttributes(htmlAttributes);
        tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
        tag.SetInnerText(labelText);
        return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
    }

Edit2: I realized it can't be named EditorFor because there already exists an overridden EditorFor that accepts an anonymous type, see here http://msdn.microsoft.com/en-us/library/ff406462.aspx.. anyway, we can name it differently, no biggies.

A: 

Have a solution given at another SO question : http://stackoverflow.com/questions/1625327/asp-net-mvc-2-editorfor-and-html-properties/3021176#3021176

Gaby
A: 

You'll need to use use a custom template that generates the input element with the attribute or you could add some javascript to the page to add the attribute client-side.

<%= Html.EditorFor( product => product.Name, "NoAutocompleteTextBox" ) %>

Then in Shared/EditorTemplates you need a NoAutocompleteTextBox.ascx that defines

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
                     new { autocomplete = "off" }) %>

or, the jQuery way, to set it on all text inputs

$(function() {
    $('input[type=text]').attr('autocomplete','off');
});
tvanfosson
This solves the problem. I would prefer a custom `EditorFor` that accepts key/value dictionary for additional attributes (class, id, etc.).. But I have no clue how it would be accomplished. The way it would be called: `<%= Html.EditorFor(product => product.Name, new { autocomplete = "off" } ) %>`
randomguy
A: 
public static MvcHtmlString EditorForAttr<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) {
    return EditorForAttr(html, expression, new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString EditorForAttr<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) {
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    string htmlFieldName = ExpressionHelper.GetExpressionText(expression);

    TagBuilder tag = new TagBuilder("input");
    tag.GenerateId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
    tag.MergeAttribute("name", htmlFieldName);
    tag.MergeAttribute("type", "text");
    tag.MergeAttribute("value", metadata.Model == null ? "" : metadata.Model.ToString()); // Not sure if this is correct.
    tag.MergeAttributes(htmlAttributes, true);
    return MvcHtmlString.Create(tag.ToString(TagRenderMode.SelfClosing));
}
randomguy
We need to get the current value, if it has one set, somehow.
randomguy