I am trying not to generate autocomplete in browser for that I am trying this
@Html.TextBoxFor(
model => model.UserName,
new { @class = "form-control", autocomplete = "off" })
@Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
which generate HTML like this
<input type="text" value="" name="UserName" id="UserName" data-val-required="User name is required." data-val="true" class="form-control" autocomplete="off">
In other views I have use the same and it is working fine. but in login view autocomplete="off"
not working. for doing so I have clear model state like this in controller
ModelState.Remove("UserName");
reference taken from the following sites
I still wonder what is wrong in my code? Your suggestion means a lot to me.
As per Bharat's answer it actually, the issue is not of the autocomplete, the issue comes from the browser's saved logins.
but we can overcome this without going to clear saved passwords of the browser.
As per my intention, all browsers doing similar duties that the field before the password is assumed as Username and it uses the last logged in data will be pasted automatically even if we do autocomplete="off"
.
The solution is to make the password's field autocomplete="new-password"
, but this will work only in HTML5 or above supported browsers only...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With