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

Ho bisogno dell'output generato per avere l'attributo di completamento automatico = "disattivato".Come disabilitare il completamento automatico del campo di input con EditorFor?

Cosa mi manca?

Edit: Sto cercando un metodo di estensione per EditorFor che accetta dizionario chiave/valore per gli attributi, quindi mi può chiamare in questo modo: <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" }) %>

Qui sto a LabelFor, ma è necessario per essere regolati per 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: mi sono reso conto che non può essere nominato EditorFor perché esiste già un EditorFor override che accetta un tipo anonimo, vedere qui http://msdn.microsoft.com/en-us/library/ff406462.aspx .. comunque, possiamo citare in modo diverso, senza biggies.

risposta

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

Abbiamo bisogno di ottenere il valore corrente, se ha un set, in qualche modo . – randomguy

4

È necessario utilizzare un modello personalizzato che genera l'elemento di input con l'attributo oppure è possibile aggiungere qualche javascript alla pagina per aggiungere l'attributo lato client.

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

Poi nel Shared/EditorTemplates avete bisogno di un NoAutocompleteTextBox.ascx che definisce

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

o, il modo jQuery, per impostarlo su tutti gli ingressi di testo

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

Questo risolve il problema Preferirei un 'EditorFor' personalizzato che accetti il ​​dizionario chiave/valore per attributi aggiuntivi (classe, id, ecc.). Ma non ho idea di come sarebbe stato realizzato. Il modo in cui verrebbe chiamato: '<% = Html.EditorFor (product => product.Name, new {autocomplete =" off "})%>' – randomguy

Problemi correlati