How to disable autocomplete in MVC Html Helper

前端 未结 4 1444
我寻月下人不归
我寻月下人不归 2020-12-28 11:44

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

相关标签:
4条回答
  • 2020-12-28 12:21

    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" />
    
    0 讨论(0)
  • 2020-12-28 12:22

    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.

    0 讨论(0)
  • 2020-12-28 12:24

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

        @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")
        }
    
        }
    
    0 讨论(0)
  • 2020-12-28 12:24

    Use autocomplete = "off"

    Html.BeginForm("ValidateLogin", "Login", FormMethod.Post, htmlAttributes: new { id = "submitForm", autocomplete = "off" })
    
    0 讨论(0)
提交回复
热议问题