How to check if checkbox is checked in ASP.NET MVC?
I have a checkbox that looks like
<%= Html.CheckBox("Remember") %>
<%= Html.Label("Remember", "Keep me signed in for two weeks.") %>
It is in shape. So when it goes to my action method, I just have a Collection variable. Now i'm trying to do
frmSign ["Remember"];
So, I tried to convert this directly to Boolean, and it failed. Then I did .toString (), and I realized that the value contained in this is equal to
"true, false"
This is normal? Why is it true, a lie? Is this true or false?
You can simply add a boolean parameter to your action:
public ActionResult LogIn(string username, string password, bool remember)
{
// You have it
}
, RouteValues [ "Remember" ] ValueProviderResult.
, :
public ActionResult LogIn(string username, string password)
{
bool remember = false;
ValueProviderResult val;
if (RouteValues.TryGetValue(key, out val))
remember = (bool)val.ConvertTo(typeof(bool));
// Now you have it
}
.
:
? , ? ?
, . checkbox MVC "False". False, .
?
- , W3.ORG , IS, ( , True False, , null).
, "true, false", , , . , "", , .
, ( W3.ORG) . , ( : , , , ..).