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

J'ai besoin que la sortie générée ait un ensemble d'attributs autocomplete = "off".Comment désactiver la saisie semi-automatique du champ de saisie avec EditorFor?

Qu'est-ce qui me manque?

Edit: Je suis à la recherche d'une méthode d'extension pour EditorFor qui accepte le dictionnaire clé/valeur pour les attributs, donc je peux l'appeler comme ceci: <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" }) %>

Ici, il est fait pour LabelFor, mais il est nécessaire pour être ajusté pour 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: je l'ai réalisé ne peut pas être nommé EditorFor parce qu'il existe déjà une EditorFor surchargée qui accepte un type anonyme, voir ici http://msdn.microsoft.com/en-us/library/ff406462.aspx .. de toute façon, nous pouvons le nommer différemment, non biggies.

Répondre

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

Nous devons obtenir la valeur actuelle, si elle a un ensemble, en quelque sorte . – randomguy

4

Vous aurez besoin d'utiliser un modèle personnalisé qui génère l'élément d'entrée avec l'attribut ou vous pouvez ajouter du javascript à la page pour ajouter l'attribut côté client.

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

Puis, en Shared/EditorTemplates vous avez besoin d'un NoAutocompleteTextBox.ascx qui définit

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

ou, comme jQuery, pour le mettre sur toutes les entrées texte

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

Cette résout le problème. Je préférerais un 'EditorFor' personnalisé qui accepte le dictionnaire clé/valeur pour les attributs supplémentaires (classe, id, etc.). Mais je n'ai aucune idée de comment cela serait accompli. La façon dont il serait appelé: '<% = Html.EditorFor (produit => product.Name, new {autocomplete =" off "})%>' – randomguy

Questions connexes