2010-07-22 43 views
5
<%= Html.EditorFor(product => product.Name) %> 

Necesito la salida generada para tener el atributo autocomplete = "off" establecido.Cómo deshabilitar la función de autocompletar del campo de entrada con EditorFor?

¿Qué me falta?

Editar: estoy buscando un método de extensión para EditorFor que acepte el diccionario de clave/valor para los atributos, por lo que se puede llamar así: <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" }) %>

Aquí está hecho para LabelFor, pero es necesario para se puede modificar para 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: me di cuenta de que no puede ser nombrado EditorFor porque ya existe una EditorFor sobrescrito que acepta un tipo anónimo, ver aquí http://msdn.microsoft.com/en-us/library/ff406462.aspx .. de todos modos, podemos nombrar de otra manera, sin Biggies.

Respuesta

2
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)); 
} 
+0

Necesitamos obtener el valor actual, si tiene un conjunto, de alguna manera . – randomguy

4

Tendrá que utilizar una plantilla personalizada que genere el elemento de entrada con el atributo o podría agregar algo de javascript a la página para agregar el atributo del lado del cliente.

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

Luego, en Común/EditorTemplates se necesita un NoAutocompleteTextBox.ascx que define

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

o, la forma en jQuery, para ponerlo en todas las entradas de texto

$(function() { 
    $('input[type=text]').attr('autocomplete','off'); 
}); 
+1

Este resuelve el problema Preferiría un 'EditorFor' personalizado que acepte el diccionario clave/valor para atributos adicionales (clase, identificación, etc.). Pero no tengo idea de cómo se lograría. La forma en que se llamaría: '<% = Html.EditorFor (product => product.Name, new {autocomplete =" off "})%>' – randomguy

Cuestiones relacionadas