tags:

views:

211

answers:

2

I have some checkbox that looks like this

  <%= Html.CheckBox("Remember") %>
  <%= Html.Label("Remember", "Keep me signed in for two weeks.") %>

This is in a form. So when it goes to my Action Method I just have a form Collection variable. Now I try to do

frmSign["Remember"];

So I tried to convert this right away to a Boolean and that failed. I then did .toString() and I realized that the value contained in this is

"true,false"

Is this normal? Why is it true,false? Is it now true or false?

A: 

Do you have two of them on the page?

Noon Silk
It would be better to make this as a comment to the question
rahul
Nope that's the first thing I looked for. I don't have any.I noticed through firebug though I have this. Not sure if this is normal or not.<input id="Remember" type="checkbox" value="true" name="Remember"/><input type="hidden" value="false" name="Remember"/>
chobo2
Ah. Well, I don't know anything about MVC-specifically, so maybe that's okay. I think the other posters answer is more helpful, so i'll leave that to you. From a pure 'Request' point of view though, that explains the two values.
Noon Silk
+6  A: 

You can just add a boolean parameter to your action:

public ActionResult LogIn(string username, string password, bool remember)
{
    // You have it
}

Additionally you can use RouteValues["Remember"] to obtain the ValueProviderResult.
So you you can do:

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
}

But I prefer the 1st option for obvious reasons.

As for the question:

Is this normal? why is it true,false? what is it now true or false?

Yes it is normal. The checkbox helper in MVC always generates hidden input with value "False". This is needed to provide False value if checkbox is not selected.
Why?
Because of according to W3.ORG the value only gets posted to server if the checkbox IS selected, otherwise it is not (so you don't know if it is True or False, eg is null).

So if you select the checkbox "true,false" comes to the server and you can safely assume the value is true.
If the checkbox is NOT selected the "false" comes to the server and you can be sure it is NOT selected.

To clarify more: the values (according to W3.ORG) with the same name are separated by comma. So it is perfectly fine to have different inputs with the same name within single form (even if the inputs are of different types: checkbox, radio, text, hidden etc).

Dmytrii Nagirniak
ya the first option works but I just don't see why having it in a frmCollection Screws it up. This is either some sort of bug or I am doing something wrong. I like the frmCollection since it just makes it so much easier to pass things into the UpdateModel. I guess I could keep frmCollection and pass in a bool parameter. It just seems to weird to do this.
chobo2
I've added the explanation.
Dmytrii Nagirniak