1

For attribute="value" I use

@Html.TextBoxFor(x => x.Email, new { type = "email", @class = "form-control",
 placeholder="Email address" })

it gives me:

<input class="form-control" id="Email" name="Email" placeholder="Email address"
 type="email" value="" />

How can I add single attribute to that, so I need required and autofocus:

<input class="form-control" id="Email" name="Email" placeholder="Email address" 
type="email" value=""  required autofocus />

Thanks!

2
  • What's wrong with using new { required = "required" }? As far as I know that would be the only way to do it. That won't give you exactly the markup you want but it'll still work. Commented Mar 31, 2014 at 20:44
  • possible duplicate of Asp.Net Mvc - Html.TextBox - Set Autofocus property Commented Mar 31, 2014 at 20:50

1 Answer 1

4

If you use string.Empty you will get an empty attribute in the generated html

@Html.TextBoxFor(x => x.Email, new { type = "email", @class = "form-control",
placeholder = "Email address", required = string.Empty, autofocus = string.Empty })
Sign up to request clarification or add additional context in comments.

3 Comments

It generates me: autofocus="" and required="", but I need just autofocus required
@ihorko that's interesting... For me, I get just "required autofocus"
autofocus="" is still valid. stackoverflow.com/questions/2998247/…

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.