2

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.

user2609980
  • 10,264
  • 15
  • 74
  • 143
  • 2
    Please post the model also. Do you have values set there (constructor or attributes)? Do you set the values in the controller? – Andrei V Nov 25 '13 at 11:47
  • Isn't this your browsers' AutoComplete kicking in? See [Is there a W3C valid way to disable autocomplete in a HTML form?](http://stackoverflow.com/questions/582244/is-there-a-w3c-valid-way-to-disable-autocomplete-in-a-html-form) to disable it. If this is after POST, see [asp.net MVC - reset the value of textarea after form submition](http://stackoverflow.com/questions/787895/asp-net-mvc-reset-the-value-of-textarea-after-form-submition). – CodeCaster Nov 25 '13 at 11:48
  • re build the code after clearing cookies from your browser, may be this should help. – manish Nov 25 '13 at 11:50
  • 3
    @manish that borders on cargo cult / shotgun debugging: performing ritual steps without understanding the underlying issue. – CodeCaster Nov 25 '13 at 11:52
  • @AndreiV I've added the Register Model. – user2609980 Nov 25 '13 at 11:56
  • @manish the cookie thing sounded very plausible, but after I removed the cookies, relogged in to StackOverflow and displayed the register page, it still displays the values. – user2609980 Nov 25 '13 at 11:59
  • 1
    WOOOOHOOO @CodeCaster I added \@AutoComplete ="off" to the TextBoxfor and Password elements and now the boxes display the placeholder values! Great. Thanks a lot! – user2609980 Nov 25 '13 at 12:02

1 Answers1

5

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!

Community
  • 1
  • 1
user2609980
  • 10,264
  • 15
  • 74
  • 143