Erwin Rooijakkers Erwin Rooijakkers - 1 year ago 65
C# Question

TextBoxFor and PasswordFor elements display default value for an unknown reason

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)]
[Display(Name = "wachtwoord")]
public string Password { get; set; }

[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.

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

<button type="submit">Register</button>

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.

Answer Source

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.,

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download