5

I'm using ASP.Net MVC (5.2.3.0) to create a form field:

@Html.TextBoxFor(
    x => x.UserName, 
    new { 
        @class = "form-control", 
        placeholder = "Enter your username", 
        required = true, 
        autofocus = true 
});

So far so good, but now I like to make the autofocus attribute conditional. How would I go about?

The autofocus attribute is a boolean attribute and W3C states:

Note: The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

The following doesn't work in MVC, because it still renders the autofocus attribute causing the browser to do the auto focus:

autofocus = String.IsNullOrEmpty(this.Model.UserName)
autofocus = String.IsNullOrEmpty(this.Model.UserName) ? null : ""`

Does anybody know how to solve this problem?

3
  • That's true, in practice all browsers ignore the value. How would I go about in getting the TextBoxFor not to render the attribute. Commented Jan 24, 2016 at 12:31
  • Possible duplicate of Set disable attribute based on a condition for Html.TextBoxFor Commented Nov 30, 2017 at 15:27
  • As a side note, the required attribute is also a boolean attribute Commented Nov 30, 2017 at 21:42

2 Answers 2

5

You could create an attribute dictionary somewhere before in the view:

@{
    var usernameAttrs = new Dictionary<string, object>
    {
        {"class ", "form-control"},
        {"placeholder  ", "Enter your username"},
        {"required", true},
    };

    if (String.IsNullOrEmpty(this.Model.UserName))
    {
        usernameAttrs.Add("autofocus", true);
    }
}

and use it in the TextBoxFor()

@Html.TextBoxFor(x => x.UserName, usernameAttrs);
Sign up to request clarification or add additional context in comments.

1 Comment

This works. But the if condition needs to be between { }, otherwise compilations fails.
0

check your condition in controller and if true set ViewBag.Tag = autofocus and then in your view

@Html.TextBoxFor(
    x => x.UserName, 
    new { 
        @class = "form-control", 
        placeholder = "Enter your username", 
        required = true, 
        ViewBag.Tag 
});

9 Comments

I don't understand this solution. Can you elaborate?
in your controller if ( !String.IsNullOrEmpty(Model.UserName)){ViewBag.Tag="autofocus"} and then you have ViewBag.tag in your view if the condition is not true then it just do nothing and if it is true it just print add autofocus as attribute.
ViewBag is Dynamic Type in C# , so you can add properties to it in runtime as we did in above example.
But I have a form with multiple fields. How would I go about in such a case?
excuse me but i am not clear about your comment , you can use autofocus only for one form field .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.