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

autocomplete = "off"속성을 설정하려면 생성 된 출력이 필요합니다.EditorFor를 사용하여 입력란의 자동 완성을 사용 중지하는 방법은 무엇입니까?

내가 무엇이 누락 되었습니까?

편집 : 나는 속성에 대한 키/값 사전을 받아 EditorFor에 대한 확장 방법을 찾고 있어요, 그래서 다음과 같이 호출 할 수 있습니다 <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" }) %> 다음

그것은 동시에, labelFor 수행됩니다 있지만에 필요 Edit2가

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)); 
    } 

EditorFor

조절할 수 : 이미 익명의 유형을 받아들이는 오버라이드 (override) EditorFor이 존재하기 때문에이 여기 http://msdn.microsoft.com/en-us/library/ff406462.aspx를 참조 EditorFor을 지정할 수 없습니다 실현 .. 어쨌든, 우리는 아니, 다르게 이름을 지정할 수 있습니다 거물.

답변

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

현재 값을 가져야합니다. . – randomguy

4

속성을 사용하여 입력 요소를 생성하는 사용자 정의 템플릿을 사용해야하거나 페이지에 자바 스크립트를 추가하여 클라이언트 측 속성을 추가 할 수 있습니다.

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

그런 다음 공유/EditorTemplates 당신이

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

또는, JQuery와 방법을 정의하는 NoAutocompleteTextBox.ascx을 필요로하는 모든 텍스트 입력에 그것을 설정하는

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

이 문제를 해결합니다. 추가 속성 (클래스, id 등)에 대한 키/값 사전을 허용하는 사용자 정의'EditorFor'를 선호합니다. 그러나 어떻게 완성 될지 전혀 모른다. 그것이라고 부르는 방법 :'<% = Html.EditorFor (product => product.Name, new {autocomplete = "off"}) %>' – randomguy

관련 문제