简体   繁体   English

System.FormatException:字符串未被识别为有效的布尔值

[英]System.FormatException: String was not recognized as a valid Boolean

I am working on an ASP.NET MVC webform and attempting to get the value of the checkbox checked by the user. 我正在使用ASP.NET MVC网络表单,并尝试获取用户选中的复选框的值。 When running the application an exception is thrown and server error is diplayed on following line of code from the controller: 运行应用程序时,将引发异常,并在控制器的以下代码行中显示服务器错误:

 supp = Convert.ToBoolean(checksupp);

I have run visual studio on debug mode and the checkbox value is getting passed but is showing the following exception : 我已在调试模式下运行Visual Studio,并且复选框值已通过,但显示以下异常:

String was not recognized as a valid Boolean. 无法将字符串识别为有效的布尔值。

The models, views and controllers are shown below and not sure how to resolve this issue. 型号,视图和控制器如下所示,不确定如何解决此问题。

Model 模型

public class WebFormXML{

  private List<string> suppressions;

    public WebFormXML()
    {
        suppressions = new List<string>();
    }

     public List<string> Suppressions
    {
        get { return suppressions; }
        set { suppressions = value; }
    }

{

View 视图

 <input type="checkbox" groupname="suppressions" name="suppressions" id="supp1" value="Prepayments" runat="server" />

Controller 控制者

  [HttpPost]
  public ActionResult createXML(FormCollection collection)
  WebFormXML wfx = new WebFormXML();
   string checksupp = collection["suppressions"];
   bool supp = true;
   supp = Convert.ToBoolean(checksupp);
   wfx.Suppressions.Add(checksupp);

In your code: 在您的代码中:

string checksupp = collection["suppressions"];

it return checksupp = "Prepayments". 它返回checksupp =“预付款”。

Convert.ToBoolean(value); //value only true or false

and with your code it is: 和您的代码是:

Convert.ToBoolean("Prepayments"); // -> error

This is occurring because the value you are expecting to read is actually "Prepayments" as opposed to a value that can be converted to a boolean (eg "true" or "false"). 发生这种情况是因为您期望读取的值实际上是“预付款”,而不是可以转换为布尔值的值(例如“ true”或“ false”)。

Checkbox Submission 复选框提交

Checkboxes by default will only submit checked values, so you shouldn't need to perform any other checks to see if other options were checked. 默认情况下,复选框只会提交选中的值,因此您无需执行任何其他检查即可查看是否选中了其他选项。 If you need to handle multiple possible selections, simply iterate through your checked options and add each of them : 如果您需要处理多个可能的选择,只需遍历选中的选项并添加每个选项即可:

[HttpPost]
public ActionResult createXML(FormCollection collection)
{
     // Build your WebForm object
     WebFormXML wfx = new WebFormXML();
     // Get your suppressed items
     var suppressions = collection["suppressions"].Split(',');
     // Add each of them to your object
     wfx.Suppressions.AddRange(suppressions);

     // Other code here
}

Example

在此处输入图片说明

You can see a very basic working example of this demonstrated here and seen below : 您可以在这里和下面看到一个非常基本的工作示例

Controller 控制者

[HttpPost]
public ActionResult Index(FormCollection collection)
{
    // Get your suppressed elements (they will come in as a comma-delimited string)
    var suppressions = collection["suppressions"];
    return Content("Properties: [" + suppressions + "] were suppressed.");
}

View 视图

@using (Html.BeginForm())
{
    <b>A</b>
    <input type="checkbox" groupname="suppressions" name="suppressions" value="A" /> 
    <b>B</b>
    <input type="checkbox" groupname="suppressions" name="suppressions" value="B" /> 
    <b>C</b>
    <input type="checkbox" groupname="suppressions" name="suppressions" value="C" /> 
    <br />
    <button type="submit">Check Suppressions</button>
}   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将字符串识别为有效的DateTime(System.FormatException)? - String was not recognized as a valid DateTime(System.FormatException)? 无法将system.formatexception字符串识别为有效的日期时间 - system.formatexception string was not recognized as a valid datetime c# 中的 DateTime 解析:获取“System.FormatException:”String 未被识别为有效的 DateTime”错误 - DateTime parsing in c#:getting a 'System.FormatException: 'String was not recognized as a valid DateTime' error 如何解决此错误:System.FormatException:&#39;字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。 - how to solve this error: System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.' System.FormatException:字符串未被识别为有效的DateTime(DateTime格式yyyyMMdd:HHmmss) - System.FormatException: String was not recognized as a valid DateTime (DateTime Format yyyyMMdd:HHmmss) System.FormatException:字符串未被识别为有效的日期时间 - 尝试转换 MM/DD/YYYY 时 - System.FormatException: String was not recognized as a valid DateTime - when trying to convert MM/DD/YYYY 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# 试图减去 DateTime “System.FormatException: String &#39;12/9/2019 12:00:00 AM&#39; 未被识别为有效的 DateTime。” - Trying to subtract DateTime "System.FormatException: String '12/9/2019 12:00:00 AM' was not recognized as a valid DateTime."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM