0

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?

5
  • 2
    autofocus or autofocus="autofocus" or autofocus="anythingAtAll" all do exactly the same thing - they set the attribute. Commented Aug 21, 2015 at 9:03
  • So is there no way to not set the attribute? Commented Aug 21, 2015 at 9:37
  • 1
    @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). Commented Aug 21, 2015 at 12:56
  • 1
    but note that you should never set the value attribute. And why do you have a model property named UserName and then add another UserName property in ViewBag? (that makes no sense at all - use the value of your model property) Commented Aug 21, 2015 at 12:59
  • Thanks @StephenMuecke Very valid points. I've made those changes as well. Commented Aug 22, 2015 at 9:32

1 Answer 1

3

Autofocus is a boolean attribute. As per the spec, if it exists, it should be listed as either autofocus="autofocus" or simply autofocus. To "turn it off" it is a case of not adding it as an attribute on the input.

Following the advice in the excellent answer here, there are a few ways to do what you want. One way is to create an extension method, and you could do it like this:

Create an extension method like so:

public static class AttributesExtensions
{
    public static RouteValueDictionary AutofocusIf(
        this object htmlAttributes, 
        bool autofocus
    )
    {
        var attributes = new RouteValueDictionary(htmlAttributes);
        if (autofocus)
        {
            attributes["autofocus"] = "autofocus";
        }
        return attributes;
    }
}

Now, when you create your username textbox:

@Html.TextBoxFor(m => m.Username, new { @placeholder = "Username", value = (string) ViewBag.Username }.AutofocusIf(String.IsNullOrWhiteSpace((string) ViewBag.Username)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jamie. This works great, however i did have to cast my ViewBag properties to actual types for the extension method to work. I updated your answer to include that.
@davey1990 - thanks for the edits. Glad you got it working :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.