asp.net mvc htmlattribute without value

别来无恙 提交于 2019-12-23 07:47:05

问题


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




回答2:


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



回答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}))....


来源:https://stackoverflow.com/questions/4135258/asp-net-mvc-htmlattribute-without-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!