[英]How to disable input field's autocomplete with EditorFor?
<%= Html.EditorFor(product => product.Name) %>
我需要將生成的輸出設置為autocomplete =“ off”屬性。
我想念的是什么?
編輯:我正在尋找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/zh-cn/library/ff406462.aspx ..無論如何,我們可以用不同的名稱命名,沒什么大不了的。
您需要使用自定義模板來生成帶有屬性的input元素,或者您可以在頁面中添加一些javascript以添加客戶端屬性。
<%= Html.EditorFor( product => product.Name, "NoAutocompleteTextBox" ) %>
然后在Shared / 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');
});
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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.