MVC Disable EditFor automatic-fill
I have recently started using MVC, and stumbled upon a problem.
So, I have a user model, which contains some variables like ID, UserName, Password, and they all correspont do a database table, which MVC completely handles.
I now have a password edit page, with a password text box, and a confirm password text box, like this:
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
@Html.Password("passwordConfirmation")
</div>
So, that works out all fine, it has 2 text boxes next to eachother.
Now here's my problem: I don't want the first password text box to be automatically filled with the data from the "Password" variable that is in the model passed along with the page.
Ofcourse there's the "hackish" way of doing it, where you just create a regular password box, and then ge开发者_如何学Got it's value later (@Html.Password("password")),
but I want to know if there's an official way of telling MVC NOT to auto-fill in the text/password box.You could try applying the autocomplete="off"
attribute to your password field:
@Html.PasswordFor(x => x.PasswordConfirmation, new { autocomplete = "off" })
Note that this is a non-standard attribute and might break HTML validation but most browsers respect it.
Also you should make sure that you are not setting any value on your view model for the PasswordConfirmation
property inside the controller action.
精彩评论