Josh
Josh

Reputation: 2790

Can you specify empty HTML attributes syntax in MVC helpers?

I'm curious... can you specify an HTML attribute that is empty or valueless? The W3 spec allows for empty attributes like so: <input type="checkbox" disabled checked>

And with MVC, you declare inputs like this:Html.CheckBoxFor(m=>m.Foo). You can add Html Attributes with the Html.CheckBoxFor(m=>m.Foo, new {@class="myCssClass"}) syntax, but you can't do Html.CheckBoxFor(m=>m.Foo, new { disabled, checked})

I know the spec also allows for self-value attributes for these kinds (e.g. new {disabled="disabled"}) and empty string values (e.g. new {disabled=""}).

I'm just curious if there is any way to specify the empty syntax.

Upvotes: 7

Views: 1602

Answers (3)

Alex from Jitbit
Alex from Jitbit

Reputation: 60942

Yes, you can. Set it to string.Empty and MVC5 will render an empty attribute.

new { disabled = string.Empty }

And yes, the HTML5 spec does allow empty attributes perfectly. XHTML is outdated.

Upvotes: 1

Evan
Evan

Reputation: 497

Late to the party, but ... this from w3schools:

"In XHTML, attribute minimization is forbidden, and the disabled attribute must be defined as: select disabled="disabled".

So for compliance, don't even try to use attribute minimization? But it seems the HTML helpers don't allow it anyway.

http://www.w3schools.com/tags/att_select_disabled.asp

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039538

No, standard HTML helpers doesn't support this. You could write a custom HTML helper and build the markup yourself.

Upvotes: 1

Related Questions