I have two input fields on my razor view. One for username and one for password.
If the username has a value passed in through the ViewBag then I don't want to set focus to the username field but rather have focus in the password field. But what happens at the moment is that the first Username input always receives the focus. The "false" value doesn't seem to do anything. I've had a google around and checked some other Stack Overflow posts but i still can't seem to find an answer on this.
I have simple code like so on my view:
@Html.TextBoxFor(m => m.Username, new { @placeholder = "Username", autofocus = (ViewBag.Username == "" ? "autofocus" : "false"), value = ViewBag.Username })
@Html.PasswordFor(m => m.Password, new { @placeholder = "Password", autofocus = (ViewBag.Username == "" ? "false" : "autofocus") })
I know i could just use JQuery to set the focus conditionally but i was hoping there would be a way through the Text Box Html Helper?
autofocusorautofocus="autofocus"orautofocus="anythingAtAll"all do exactly the same thing - they set the attribute.@Html.TextBoxFor(m => m.Username, Model.Username == "" ? (object)new { @placeholder = "Username", autofocus = "autofocus" } : (object)new { @placeholder = "Username" })and similar for the second textbox (in reverse).valueattribute. And why do you have a model property namedUserNameand then add anotherUserNameproperty inViewBag? (that makes no sense at all - use the value of your model property)