I’m trying to output the following html using an html helper in asp.net mvc 2:
<input type="text" id="Name" name="Name" value="" autofocus />
I’m using this code now:
<%= Html.TextBoxFor(x => x.Name) %>
and I’ve tried
// results in <input ... autofocus="" />
<%= Html.TextBoxFor(x => x.Email, new { autofocus="" }) %>
// does not compile
<%= Html.TextBoxFor(x => x.Email, new { autofocus=null }) %>
Is there any way to accomplish what I’m looking for using an html helper?
I think you’ll have to write your own helper for this. The default helpers output key/value pairs. Under the hood it uses a TagBuilder to construct the tag. For tag attributes it uses the format
{0}="{1}"to format the key/value pairs for the attributes and their values.