How can I generate the following input
element using standard HTML helpers and Razor view engine:
<input id="Foo" name="Foo" type="text" autofocus />
Can I use the standard HTML helper or do I have to write my own?
Any help would be greatly appreciated!
You can pass additional HTML attributes to the TextBoxFor method:
@Html.TextBoxFor(m => m.Foo, new { autofocus="autofocus"})
Edit:
You can get only autofocus=""
with:
@Html.TextBoxFor(m => m.Foo, new { autofocus=""})
All the built in helpers are using the TagBuilder class's MergeAttribute method internally, and it only supports attributes in the following format : key="value"
.
So if you need only autofocus
you need to write your own helper with a custom html builder.
I think autofocus="autofocus" is also valid see: http://www.w3schools.com/html5/att_input_autofocus.asp so you can use the htmlAttributes argument like so:
@Html.TextBox("Foo", null, new { autofocus = "autofocus" })
I think you cannot use the standard HTML helpers if you really just want autofocus, you would have to do something like this:
@Html.Raw("<input id=\"Foo\" name=\"Foo\" type=\"text\" autofocus />")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With