Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a checkbox on my page, created as such:

@Html.CheckBoxFor(x => x.MyBoolValue)

When i am Checking it to True and submiting this view then it is generating an error.

and i have found the "false" value in the controller.

The inner exception is:
{"true,false is not a valid value for Boolean."}

So Please suggest me how to resolve it..
Posted
Updated 5-Feb-14 4:03am
v2
Comments
Thomas Daniels 5-Feb-14 9:59am    
What's the value of x.MyBoolValue?
Anubhava Dimri 5-Feb-14 10:02am    
It's Boolean Variable by default it's value is false. When i am checked the checkbox and submit it then error is generating...

When you are using a checkbox on your view in MVC then that checkbox also add a extra hidden filed for same name and when you go submit then you will get hidden filed value of check box.

Now check box and hidden filed have same name then a single request variable has both values comma separate like "true,false" but first value always for checkbox and second value for hidden filed so you can split this and get check box value.

C#
[HttpPost]
      public ActionResult Index(FormCollection form)
      {
          bool MyBoolValue= Convert.ToBoolean(form["MyBoolValue"].Split(',')[0]);
          return View();
      }


other way

View is

HTML
@Html.CheckBoxFor(x => x.MyBoolValue, Model.MyBoolValue)


C#
[HttpPost]
      public ActionResult Index(Modal model)
      {
          bool MyBoolValue= Convert.ToBoolean(model.MyBoolValue);
          return View();
      }
 
Share this answer
 
v3
Use Convert.ToBoolean to convert the values to boolean.
Then do the comparison.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900