8

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?

1
  • I think it's just better to use regular Html instead of Html helpers, in the end you end up writing the same amount of code and i don't see what benefit you get from the helper. Commented Nov 9, 2010 at 16:50

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well I know, I just wanted to know whether it is possible with the standard helpers.
I updated my answer after some research. According to MSDN the attributes must be in name/value pairs. I would hope that this will change in the future now that HTML5 is becoming more popular. I have not looked into MVC3 yet to see if this is still the case.
5

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" ...>

4 Comments

And there is the problem. If I use what you suggest, I would end up with hidden="" in the code. But I want just hidden.
I'm not sure if this is possible using the Html helpers. However, there is certainly nothing wrong with passing up on the helpers and just putting your form directly in the markup.
@Chevex I'm thinking the same thing. I've posted an answer that does exactly that.
Updated my answer. According to the specc, hidden="" should be valid. But if you just want hidden, then the other answer is better
3

Just use string.Empty, it will output with no attribute. At least it does in MVC 5.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.