bmavity
bmavity

Reputation: 2508

How do you add a boolean attribute in asp.net mvc?

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?

Upvotes: 0

Views: 946

Answers (3)

mezmo
mezmo

Reputation: 2480

I know this is long ago and far away, but wasn't there an old rule that you could enter such attributes as

required="required"

This appears to work in FireFox, Chrome and Opera, but not Safari, nor of course IE8, which is my companies standard, (queue trombone...Waa, waa, waa. ;)

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

Html helpers generate XHTML. What you are trying to achieve is:

  1. Not XHTML (attributes need to have values, even empty)
  2. Not W3C standard (no autofocus attribute for the input tag)

If you want to generate such markup you will need to do it manually or write your own helper.

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532455

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.

Upvotes: 1

Related Questions