错误:true,false不是布尔值的有效值... [英] Error : true,false is not a valid value for Boolean ...

查看:765
本文介绍了错误:true,false不是布尔值的有效值...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个复选框,如下所示:



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



当我检查它为True并提交此视图时,它会产生错误。



我找到了虚假的控制器中的值。



内部异常是:

{true,false不是布尔值的有效值。}



所以请建议我如何解决它..

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

推荐答案

当您在MVC中使用视图复选框时,那么复选框还为同一个名称添加了一个额外的隐藏文件,当你提交时,你将获得隐藏的复选框的归档值。



现在复选框和隐藏的文件具有相同的name然后单个请求变量将两个值逗号分开,如true,false,但第一个值始终用于复选框,第二个值用于隐藏字段,因此您可以拆分并获取复选框值。



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.

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





其他方式



查看是





other way

View is

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







[HttpPost]
      public ActionResult Index(Modal model)
      {
          bool MyBoolValue= Convert.ToBoolean(model.MyBoolValue);
          return View();
      }


使用 Convert.ToBoolean 将值转换为布尔值。

然后进行比较。
Use Convert.ToBoolean to convert the values to boolean.
Then do the comparison.


这篇关于错误:true,false不是布尔值的有效值...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆