在IE和Chrome的MVC中禁用自动填充

狮子座

我有我的login.cshtml页面,其中密码文本框显示了自动完成选项,我需要禁用此选项。下面是我的代码。

<div id="login">
    @using (Html.BeginForm(MVC.Account.Login())) {

        <div>@Html.ValidationSummary(true)</div>

        <div>@Html.LabelledTextBoxFor(m => m.Username)</div>
        <div>@Html.LabelledPasswordFor(m => m.Password)</div>
    }
</div>

如何禁用自动完成选项,该选项应在IE和Chrome中都可以使用?

如果我在下面的代码中使用此代码,那么我将收到错误消息“无重载方法带有LabelledPasswordFor2个参数”。

@Html.LabelledPasswordFor(m => m.Password, new { autocomplete = "off" })

public static MvcHtmlString LabelledPasswordFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
    var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    var displayName = html.RawDisplayNameFor(expression);
    var editor = html.PasswordFor(expression, metadata.IsRequired ? (object)new { placeholder = displayName, @class = "requiredInput" } : new { placeholder = displayName });
    var validator = html.ValidationMessageFor(expression);
    return new MvcHtmlString(editor.ToHtmlString() + validator.ToHtmlString());
    }
彼得·B

由于LabelledPasswordFor是您自己的实现,因此您应该可以将其更改为support autocomplete下面是一个例子。

我选择使用调用它true/false,然后将其转换为,"on" / "new-password"因此调用者不必担心该属性的确切细节。

public static MvcHtmlString LabelledPasswordFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression,
    bool autocomplete = true)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    var displayName = html.RawDisplayNameFor(expression);

    var htmlAttr = new Dictionary<string, object>();
    htmlAttr.Add("placeholder", displayName);
    htmlAttr.Add("autocomplete", autocomplete ? "on" : "new-password");
    htmlAttr.Add("class", metadata.IsRequired ? "requiredInput" : "");
    var editor = html.PasswordFor(expression, htmlAttr);

    var validator = html.ValidationMessageFor(expression);
    return new MvcHtmlString(editor.ToHtmlString() + validator.ToHtmlString());
}

用法:

<div>@Html.LabelledPasswordFor(m => m.Password, autocomplete: false)</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章