Asp.net mvc html attribute without value

I am trying to create a hidden form according to the HTML5 specification where the hidden attribute is used without value. Now I do not know how to force it in asp.net mvc

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

since the above is not compiled with

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

Does anyone know a way out?

EDIT

Just out of curiosity using the default html helpers?

+6
html5 asp.net-mvc asp.net-mvc-2
source share
3 answers

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

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

UPDATE:

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

+5
source share

The HtmlAttribute collection consists of key-value pairs and here hidden is the key. You must give it meaning too. As you did now, the compiler interprets it as if you were referring to a hidden variable that you did not define.

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

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

According to specc:

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

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

If an attribute is present, its value must be an empty string or a value that is ASCII case insensitive for the attribute of the canonical name, without leading or trailing spaces.

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

 <... hidden ...> <... hidden="" ...> <... hidden="hidden" ...> 
+4
source share

Just use string.Empty, it will be output without attribute. At least this happens in MVC 5.

 @using (Html.BeginForm("Login", "Main", FormMethod.Post, new { name = "loginForm", ng_controller = "loginController", @class = "form-horizontal", novalidate = string.Empty})).... 
+2
source share

All Articles