Why is my hidden input writing: value=”value” instead of true/false?

I have an MVC4 site, with (as part of a hidden form):

<input name="somefield" type="hidden" value="@ViewBag.Test"/>

The value of ViewBag.Test is true. The form field is posting to an input parameter of the form:
public ActionResult SomeAction(bool somefield = false, ...)

but somefield is always false. Upon investigating, I see that the source code has:
<input name="somefield" type="hidden" value="value"/>

However, I know this used to work. What has happened, and what can I do?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This behaviour changed between MVC3 and MVC4. In MVC3, if you have:

<input name="somefield" type="hidden" someprop="@(SomeBooleanExpression)"/>

it would write very literally:
<input name="somefield" type="hidden" someprop="True"/>

However, in MVC4, it follows the “checkbox” etc rules, so if the value is true you get:
<input name="somefield" type="hidden" someprop="someprop"/>

and if it is false it is omitted completely:
<input name="somefield" type="hidden"/>

To get around this, consider .ToString():
<input name="somefield" type="hidden"

which then follows string rules rather than boolean rules.

Method 2

initialized Boolean values bool something =false;

then convert this value into string like,

<input name=”somefield” type=”hidden” value=”@something.ToString()>


initialized string something =”false”;
< input name=”somefield” type=”hidden” value=”@something” >

then we can read

    public ActionResult Somemethod(bool IsFromDB) => you can get Boolean value here 

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x