Reputation:
Ok,
I would in normal asp.net use a theme to turn off autocomplete on all text boxes on an entire site. However i cannot do this on MVC because nothing in the theme .skin files seems to work.
I have this in my .skin file:
<asp:TextBox runat="server" autocomplete="off" />
however this does not render at all, of course because this is not how MVC works. Anyway is there any way i can get this sort of thing to work. The site i am trying to do it on is too big to warrant changing every textbox or creating a new HTML helper to solve the issue?
Anyone got any ideas?
Upvotes: 11
Views: 43599
Reputation: 789
Use following jQuery Code
$(document).ready(function() {
$("form,input").attr("autocomplete","off");
});
Upvotes: 0
Reputation: 129
ASP.net MVC HTML Attribute: Which is having type="text" Managed by JQUERY code
@Html.TextBoxFor(model => model.FirstName, new {@id = "txtFirstName"})
<script language="javascript" type="text/javascript">
$(document).ready(function () {
try {
$("input[type='text']").each(function () {
$(this).attr("autocomplete", "off-1");
});
}
catch (e) { }
});
</script>
Upvotes: 0
Reputation: 129
Instead of off- write anything @autocomplete='write-anything-instead-of-off'
@Html.TextBoxFor(model => model.FirstName, new { placeholder = "First Name", @class = "form-control", @id = "txtFirstName", @autocomplete = "new-FirstName"})
Upvotes: 1
Reputation: 71
Use autocomplete = "disable-autocomplete"
The autocomplete= "off" is no longer supported.
Upvotes: 0
Reputation: 15999
Simply add this to the _Layout.cshtml document end
<script>
$("input[type='text']").attr("autocomplete","off");
</script>
Upvotes: 1
Reputation: 1
@Html.TextBox("Address","",new { @class = "classname", autocomplete = "off" })
Upvotes: 0
Reputation:
Cheers for the answers guys, however those solutions would really require me to edit all the forms in the site, if you saw the use of MVC we are doing, you would understand.
Anyway i opted for this way, in the master page i added this script:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
try {
$("input[type='text']").each(function(){
$(this).attr("autocomplete","off");
});
}
catch (e)
{ }
});
</script>
I know if javascript is disabled this is worthless however to be honest if javascript is disabled the users won't be able to use half the site.
Upvotes: 14
Reputation: 59234
MVC does not have server controls like plain old ASP.NET. Therefore no server processing is done on your controls. They are rendered to the client exactly how you type them. Themes are not something you will use in MVC, because they apply to ASP.NET server controls and you won't be using those here. That said, HTML helpers do get processed by the server as the view is rendered. You will need to add autocomplete="off" to the actual HTML control using the html properties overload.
@Html.TextBoxFor(x => x.Something, new { autocomplete="off" } )
Or whatever the actual HTML attribute is that gets rendered when you set autocomplete="off" in the asp.net server control.
EDIT: One option to affect all text boxes would be to create your own Html helper method. Just create an extension method Like this:
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static MvcHtmlString NoAutoCompleteTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
return html.TextBoxFor(expression, new { autocomplete="off" });
}
Then you can just do:
Html.NoAutoCompleteTextBoxFor(x => x.Something)
Upvotes: 27
Reputation: 109037
AFAIK, you cannot do autocomplete = off
with css and it has to be a html attribute and hence there is nothing you can that would affect all Textboxes. One thing you can do is add the attribute to the form like this (it will address all textboxes in the current form)
Html.BeginForm(action,controller, FormMethod.Post, new { autocomplete="off"})
or create a custom Helper extension method that is similar to BeginForm
that adds this html attribute internally.
Upvotes: 10