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

Tôi cần đầu ra được tạo để có tập hợp thuộc tính tự động điền "tắt".Làm cách nào để vô hiệu hóa tính năng tự động hoàn thành của trường đầu vào bằng EditorFor?

Tôi đang thiếu gì?

Edit: Tôi đang tìm một phương pháp mở rộng cho EditorFor chấp nhận từ điển chìa khóa/giá trị cho các thuộc tính, vì vậy tôi có thể gọi nó như thế này: <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" }) %>

Ở đây nó được thực hiện cho LabelFor, nhưng nó là cần thiết để được điều chỉnh cho 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: tôi nhận ra nó không thể được đặt tên EditorFor vì đã có tồn tại một EditorFor ghi đè chấp nhận một loại vô danh, xem tại đây http://msdn.microsoft.com/en-us/library/ff406462.aspx .. dù sao, chúng ta có thể đặt tên cho nó khác nhau, không có biggies.

Trả lời

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

Chúng ta cần lấy giá trị hiện tại, nếu nó có một bộ, bằng cách nào đó . – randomguy

4

Bạn cần phải sử dụng mẫu tùy chỉnh tạo phần tử đầu vào với thuộc tính hoặc bạn có thể thêm một số javascript vào trang để thêm thuộc tính phía máy khách.

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

Sau đó, trong Shared/EditorTemplates bạn cần một NoAutocompleteTextBox.ascx định nghĩa

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

hay, cách jQuery, để thiết lập nó trên tất cả đầu vào văn bản

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

này giải quyết vấn đề. Tôi muốn có một 'EditorFor' tùy chỉnh chấp nhận từ điển khóa/giá trị cho các thuộc tính bổ sung (lớp, id, vv) .. Nhưng tôi không có đầu mối làm thế nào nó sẽ được hoàn thành. Cách nó sẽ được gọi là: '<% = Html.EditorFor (product => product.Name, new {autocomplete =" off "})%>' – randomguy

Các vấn đề liên quan