I have a checkbox and button:
@using(Html.BeginForm())
{
<div id="search-block">
<input type="checkbox" name="showAll" id="showAll">Include completed
<input type="submit" value="Apply"/>
</div>
}
and second thing in controller:
[HttpPost]
public ActionResult Index(FormCollection collection)
{
var showAll = collection["showAll"];
TempData["showAll"] = showAll;
...
something
...
}
It's actually working, BUT:
If checkboxes are not checked, I am receiving null (doesn't bother me much).
If checkboxes are checked, I am receiving "on" from FormCollection
, and this is not what I need. I want to receive true or false.
How can I do this?
Note: Unlike other input controls, a checkbox's value is only included in the submitted data if the checkbox is currently checked . If it is, then the value of the checkbox's value attribute is reported as the input's value.
Return Value: It returns a string value that represents the value of the value attribute of a input checkbox field.
Try this
bool MyBoolValue= collection["showAll"].Count > 1;
when 1 then false
when 2 then true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With