in asp.net mvc, it seems like checkboxes bind to array of strings (if they are checked). is there any view control that will bind to a boolean in my controller action
public ActionResult Go(bool isBold)
{
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In your view you can use the
Html.CheckBoxhelper:This will in fact render two HTML input fields:
This is why it might appear that “bool” binds to arrays of booleans, which isn’t exactly true.
The reason there are two inputs is that checkboxes that are unchecked post no value at all. That means ASP.NET MVC can’t tell the difference between “this wasn’t posted at all” as opposed to “this was posted but it was not checked.”
By having two inputs ASP.NET MVC is guaranteed to always get at least one input. Then it just looks at the first. Here’s what happens:
If the checkbox is checked, it sees “true,false” and picks the first value:
true.If the checkbox is unchecked it sees “false” and picks the first value:
false.You can still use other helpers with boolean input values, such as
Html.TextBoxorHtml.DropDownList. The only thing that ASP.NET MVC cares about is that the first posted value with that name either says “true” or “false”.