asp.net-mvc 如何在 MVC Html Helper 中禁用自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3015803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:21:47  来源:igfitidea点击:

How to disable autocomplete in MVC Html Helper

asp.net-mvc

提问by Matt

I have a typical ADO.NET EF-driven form that allows the user to input a date. I have put a jQuery datepicker on it but when the user goes to select a date the browser shows some other entries in a dropdown. How do I turn off that dropdown? In traditional ASP.NET I would put autocomplete="off". Not sure of the MVC equivalent.

我有一个典型的 ADO.NET EF 驱动的表单,它允许用户输入日期。我在它上面放了一个 jQuery datepicker,但是当用户去选择一个日期时,浏览器会在下拉列表中显示一些其他条目。如何关闭该下拉菜单?在传统的 ASP.NET 中,我会设置 autocomplete="off"。不确定 MVC 等效项。

<div class="editor-field">
    <%= Html.TextBoxFor(model => model.date, new { @class = "aDatePicker" })%>
    <%= Html.ValidationMessageFor(model => model.date) %>
</div> 

回答by Darin Dimitrov

Try this:

尝试这个:

<%= Html.TextBoxFor(
    model => model.date, 
    new { @class = "aDatePicker", autocomplete = "off" }
)%>

It will generate markup that is close to the following:

它将生成接近以下内容的标记:

<input type="text" id="date" name="date" class="aDatePicker" autocomplete="off" />

回答by John Wu

Couple of points

几点

  1. If you've more or less already written the site and you don't want to go back and modify all your cshtml to disable autocomplete (we would have had to go back and change hundreds of lines of code throughout the site), you can disable it via Javascript with a ready handler, like so:

    //Disable autocomplete throughout the site
    $(document).ready(function() {
        $("input:text,form").attr("autocomplete","off");
    })
    
  2. From what I've read you need to disable it at both the form and the textbox level in order for it to work on all versions of Firefox and IE.

  1. 如果您或多或少已经编写了该站点并且您不想返回并修改所有 cshtml 以禁用自动完成功能(我们将不得不返回并更改整个站点的数百行代码),您可以通过带有就绪处理程序的 Javascript 禁用它,如下所示:

    //Disable autocomplete throughout the site
    $(document).ready(function() {
        $("input:text,form").attr("autocomplete","off");
    })
    
  2. 从我读过的内容来看,您需要在表单和文本框级别禁用它,才能在所有版本的 Firefox 和 IE 上运行。

回答by Karthik Lal

Use autocomplete = "off"

使用自动完成=“关闭”

Html.BeginForm("ValidateLogin", "Login", FormMethod.Post, htmlAttributes: new { id = "submitForm", autocomplete = "off" })

回答by PCPGMR

I used Darin's above and applied it successfully to a demo:

我在上面使用了 Darin 并将其成功应用于演示:

    @model RequestAQuote.Models.RequestAQuoteModel
    @{
        ViewBag.Title = "Request a Free Quote";
        List<string> ProjectTypes = new List<string>();
        ProjectTypes.Add("Electrical Wiring");
        ProjectTypes.Add("Install Breakers");
        ProjectTypes.Add("Ceiling Fan");
        ProjectTypes.Add("Replace Light");
        ProjectTypes.Add("Replace Outlet");    
    }

    <h2>Request A Quote</h2>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary()
        <fieldset>
            <legend>Request a Quote Form</legend>
            <ol>
                <li>
                    @Html.LabelFor(m => m.ProjectType)
                    @Html.DropDownListFor(m => m.ProjectType, new MultiSelectList(ProjectTypes))
                </li>
                <li>
                    @Html.LabelFor(m => m.ContactName)
                    @Html.EditorFor(m => m.ContactName, new { autocomplete = "off" })
                </li>
                <li>
                    @Html.LabelFor(m => m.DaTimePhone)
                    @Html.EditorFor(m => m.DaTimePhone, new { autocomplete = "off" })
                </li>
                <li>
                    @Html.LabelFor(m => m.Email)
                    @Html.EditorFor(m => m.Email, new { autocomplete = "off" })
                </li>
                <li>
                    @Html.LabelFor(m => m.ProjectDescription)
                    @Html.EditorFor(m => m.ProjectDescription, new { autocomplete = "off" })
                </li>
            </ol>
            <input type="submit" value="Request Quote" />
        </fieldset>
    }

    @section scripts 
    {
        @Scripts.Render("~/bundles/jqueryval")
    }

    }