TextBoxFor and PasswordFor elements display default value for an unknown reason

12,507

Thanks to the suggestion by CodeCaster I found a solution. By adding: @AutoComplete ="off" to the TextBoxfor and PasswordFor elements the problem is solved. E.g.,

<li>
    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", 
                                            @placeholder = "Volledige naam",
                                            @Autocomplete= "off"})
</li>

YEAH!

Share:
12,507
user2609980
Author by

user2609980

Updated on September 12, 2022

Comments

  • user2609980
    user2609980 over 1 year

    I have a problem with a view where a new user can be created. There are default values in the textboxes and I have no idea how to get rid of them.

    /edit The register model looks like this:

       public class RegisterModel
        {
            [Required(ErrorMessage = "*")]
            [Display(Name = "Naam")]
            public string UserName { get; set; }
    
            [Required(ErrorMessage = "*")]
            [Display(Name = "E-mailadres")]
            public string Email { get; set; }
    
            [Required(ErrorMessage = "*")]
            [StringLength(100, ErrorMessage = "Het {0} moet op zijn minst {2} tekens lang zijn.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "wachtwoord")]
            public string Password { get; set; }
    
            [DataType(DataType.Password)]
            [Display(Name = "Bevestig wachtwoord")]
            [Compare("Password", ErrorMessage = "De twee wachtwoorden komen niet overeen.")]
            public string ConfirmPassword { get; set; }
        }
    

    The views looks like this and I want only the text of the @placeholder element in the box.

    <fieldset>
        <legend>Register new user</legend>
        <ol>
            <li>
                @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "Volledige naam"})
            </li>
            <li>
                @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "E-mail "})
            </li>
            <li>
                @Html.PasswordFor(m => m.Password, new { @class = "form-control", @placeholder = "Wachtwoord" })
            </li>
            <li>
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control", @placeholder = "Bevestig wachtwoord"})
            </li>
    
        </ol>
        <button type="submit">Register</button>
    </fieldset>
    

    Unfortunately, the output looks like this:

    something is wrong here (Apologies for the Dutch)

    The username where I am currently logged in with (a_random_username) is displayed in the box and the same goes for the password field.

    I've tried to place a @Value = "" element in the new {} element of the TextBoxFor and PasswordFor elements with no success. I tried to create an empty RegisterModel in the model and pass it to the view with no success. And I tried to update the default values in the model with no success.

    I have absolutely no idea why it automatically fills in my own username and password.

    I would be very grateful if someone could release me from this misery and explain to me how I can get my own chosen default value to display in the textboxes.