0

I am testing this on chrome. I have set chrome to remember user name and passwords. I have a very simple MVC 5 web application where i have login screen and also screen to create user. When user log in, I let chrome save user name and password.

Problem is that when I try to create new user, its not coming user name as blank, but still showing user name that I logged in with.

I want to do in view and not through controller default vault. I tried using below example link on SO, but my code is still not working

how to set default value HTML.EditorFor()

Here is the code that I tried and its not working.

                    <div class="form-group">
                        <div class="col-md-12">
                            @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label" })
                            <br />
                            @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", @Value ="" } })
                            @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
                        </div>
                    </div>

Please advice what should I do to fix this. You you.

ary
  • 829
  • 1
  • 11
  • 30
  • 1
    Looks to me like your problem is caused by Chrome form auto-fill. See [Disabling chrome autofill](http://stackoverflow.com/questions/15738259/disabling-chrome-autofill). – Georg Patscheider Dec 21 '16 at 16:56
  • Yes I understand that. But questions is whats the work around without disabling chrome auto fill? – ary Dec 21 '16 at 16:57
  • Try to set `autocomplete="new-password"` (passed as htmlAttributes) for the individual field on which you want to prevent autofill. Got this from the link I posted. – Georg Patscheider Dec 21 '16 at 17:02

1 Answers1

1

Easiest way around this is to simply change the name of the property in your model from UserName to NewUserName. That way the autofill won't associate autofill entries between the two different fields. Your create and login model are different anyway.

Or, you could also try changing the name of the textbox in your View.

@Html.EditorFor(m => m.UserName, new {htmlAttributes = new { @Name="NewUserName", @id="NewUserName", @class = "form-control", @Value =""}})

You would need to add a parameter in your controller for NewUserName next to your other model parameters.

Carter Medlin
  • 10,482
  • 4
  • 55
  • 65
  • Thanks Carter. I have only one model. I do have separate controllers. But will study to see how can I have different model for each (create,Login etc) – ary Dec 21 '16 at 20:08
  • 1
    You could also just add a separate property to the same model. As long as the property name is different, it won't confuse the autofills. – Carter Medlin Dec 22 '16 at 00:16
  • Thanks Carter. I did thought about that. But my model is auto generated. And sorry earlier I meant separate views and not separate controller. – ary Dec 22 '16 at 14:08
  • 1
    How about changing the name and id of the field? See my edit. – Carter Medlin Dec 22 '16 at 19:19