如何使用 EditorFor 禁用输入字段的自动完成功能?

发布于 2024-09-10 18:52:29 字数 1801 浏览 11 评论 0原文

<%= Html.EditorFor(product => product.Name) %>

我需要生成的输出设置 autocomplete="off" 属性。

我缺少什么?

编辑: 我正在寻找 EditorFor 的扩展方法,它接受属性的键/值字典,因此我可以这样调用它: <%= Html.EditorFor(product => product.Name, new { autocomplete = " off" } ) %>

需要调整一下

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

这里是针对LabelFor完成的,但是针对EditorFor Edit2 : 我意识到它不能命名为 EditorFor,因为已经存在一个接受匿名类型的重写 EditorFor,请参见此处 http://msdn.microsoft.com/en-us/library/ff406462.aspx..无论如何,我们可以以不同的方式命名它,没什么大不了的。

<%= Html.EditorFor(product => product.Name) %>

I need the generated output to have autocomplete="off" attribute set.

What I'm missing?

Edit:
I'm looking an extension method for EditorFor that accepts key/value dictionary for attributes, so I can call it like this: <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" } ) %>

Here it is done for LabelFor, but it is needed to be adjusted for 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:
I realized it can't be named EditorFor because there already exists an overridden EditorFor that accepts an anonymous type, see here http://msdn.microsoft.com/en-us/library/ff406462.aspx.. anyway, we can name it differently, no biggies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2024-09-17 18:52:29

您需要使用自定义模板来生成带有该属性的输入元素,或者您可以向页面添加一些 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');
});

You'll need to use use a custom template that generates the input element with the attribute or you could add some javascript to the page to add the attribute client-side.

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

Then in Shared/EditorTemplates you need a NoAutocompleteTextBox.ascx that defines

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

or, the jQuery way, to set it on all text inputs

$(function() {
    $('input[type=text]').attr('autocomplete','off');
});
神经大条 2024-09-17 18:52:29
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));
}
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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文