Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable autocomplete in MVC Html Helper

Tags:

asp.net-mvc

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.

<div class="editor-field">     <%= Html.TextBoxFor(model => model.date, new { @class = "aDatePicker" })%>     <%= Html.ValidationMessageFor(model => model.date) %> </div>  
like image 955
Matt Avatar asked Jun 10 '10 15:06

Matt


People also ask

How can stop auto fill TextBox in asp net MVC?

In traditional ASP.NET I would put autocomplete="off".

How do I turn off TextBox suggestions?

Click the Display tab. Do one of the following: To enable AutoComplete for the text box, select the Enable AutoComplete check box. To disable AutoComplete for the text box, clear the Enable AutoComplete check box.


2 Answers

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.

like image 38
John Wu Avatar answered Sep 18 '22 15:09

John Wu


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" /> 
like image 82
Darin Dimitrov Avatar answered Sep 20 '22 15:09

Darin Dimitrov