[英]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.