5
<%= 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/en-us/library/ff406462.aspx .. 无论如何,我们可以换个名字,没什么大不了的。

4

2 回答 2

4

您需要使用自定义模板来生成带有属性的输入元素,或者您可以向页面添加一些 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');
});
于 2010-07-22T12:16:44.077 回答
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));
}
于 2010-07-22T13:04:29.387 回答