如何使用EditorFor禁用输入字段的自动完成功能?
<%= Html.EditorFor(product => product.Name) %>
我需要生成的输出具有autocomplete =“off”属性集。如何使用EditorFor禁用输入字段的自动完成功能?
我失踪了什么?
编辑: 我在寻找EditorFor接受键/值字典属性的扩展方法,这样我就可以这样调用:<%= Html.EditorFor(product => product.Name, new { autocomplete = "off" }) %>
这是为LabelFor完成,但它是需要对于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进行调整: 我意识到,因为已经存在一个接受匿名类型的重载EditorFor它不能被命名EditorFor,看到这里http://msdn.microsoft.com/en-us/library/ff406462.aspx ..反正,我们可以以不同的名字,没有BIGGIES。
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));
}
您需要使用自定义模板来生成带有属性的输入元素,或者您可以在页面中添加一些javascript以添加属性客户端。
<%= Html.EditorFor(product => product.Name, "NoAutocompleteTextBox") %>
然后在共享/ EditorTemplates你需要一个NoAutocompleteTextBox.ascx定义
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
new { autocomplete = "off" }) %>
,或者jQuery的方式,将其放置在所有文本输入
$(function() {
$('input[type=text]').attr('autocomplete','off');
});
这解决了这个问题。我更喜欢自定义的'EditorFor',它接受用于附加属性(class,id等)的键/值字典。但我不知道它将如何实现。它会被调用的方式:' product.Name,new {autocomplete =“off”})%>' – randomguy 2010-07-22 12:24:38
我们需要获取当前值,如果它有一个集合, 。 – randomguy 2010-07-22 13:05:14