Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc htmlattribute without value

I am trying to create a hidden form according to the HTML5 spec, where the hidden attribute is used without a value. Now I have no idea how to force it into asp.net mvc's

<% Html.BeginForm("ChangeContent", "Content", FormMethod.Post, new {hidden}); %>

method as the one above does not compile with

Compiler Error Message: CS0103: The name 'hidden' does not exist in the current context

Any one knows a way out?

EDIT

Just out of curiosity using default html helpers?

like image 659
Trimack Avatar asked Nov 09 '10 15:11

Trimack


2 Answers

I have a feeling that the default BeginForm method will not do what you want. You can either create a new BeginForm method that will output the <form> tag as you wish, or just write the <form> tag manually in HTML and fill in the URL using the routing engine:

<form action="<%: Url.Action("ChangeContent", "Content") %>" method="post" hidden>
    ...
</form>

UPDATE:

To answer your question edit, this is not possible using the standard helpers. Here is the reference on MSDN: http://msdn.microsoft.com/en-us/library/dd492714.aspx. According to the documentation, the attributes must be name/value pairs.

like image 83
mkchandler Avatar answered Sep 19 '22 16:09

mkchandler


The HtmlAttribute collection consists of key-value pairs and here hidden is the key. You have to give it a value too. As you've written it now, the compiler interprets it as if you're referencing the variable hidden, which you haven't defined.

If you want hidden = "" in your HTML, use

<% Html.BeginForm("ChangeContent", "Content", FormMethod.Post, new { hidden = "" }); %>

According to the specc:

hidden is a boolean attribute. Boolean attributes can be indicated in several ways [ref]:

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

In other words, the hidden attribute can be represented in three ways

<... hidden ...>
<... hidden="" ...>
<... hidden="hidden" ...>
like image 28
Simen Echholt Avatar answered Sep 20 '22 16:09

Simen Echholt