2

Hi,

I know that its possible to use Html.CheckBoxFor(c=>c.MyBool) to get the default binder to bind correct value to an model object parameter in an control action(strong typed view).

If I however need to add a "rememberme" checkbox in a form on the masterpage, this will mean that there is no strong type to use.

Say that the Logon action takes a object of the following class

public class LogOnModel
    {
        [Required]
        [DisplayName("User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        public string Password { get; set; }

        [DisplayName("Remember me?")]
        public Boolean RememberMe { get; set; }
    }

To get the default binder to map to UserName and Password we could just create inputs that has the correct names (UserName/Password). This is however not possible with the RememberMe property. To get this working I hade to ad a hidden field with the name RmemberMe and then set this input with a javascript like this :

$(document).ready(function () {

            $('input[id*=chkbRemember]').click(function () {
                if ($('input[id*=chkbRemember]').attr('checked')) {
                    $('input[id *= RememberMe]').val("True");
                }
                else {
                    $('input[id *= RememberMe]').val("False");
                }
            });
        });

This will work but is it really the right way?

BestRegards

Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Why is it not possible to name it "RememberMe"? Are you using Html.CheckBox or just the `input type="checkbox"`? – dotjoe Apr 11 '11 at 18:34
  • If I just name it RememberMe the RememberMe Property will not be set. I am just using input and not Html.CheckBox. Its possible that Html.Checkbox will generate the hidden field? – Banshee Apr 11 '11 at 18:47
  • Yes, the HtmlHelper CheckBox will generate a checkbox and a hidden field with the same name to handle the unchecked/false value. – dotjoe Apr 11 '11 at 18:57
  • Yes! thanks! That wokes fine. Strange that I didn´t found this earlier! I did google it and found solutions like mine but I supose that that was with a vary early version of MVC. – Banshee Apr 11 '11 at 19:43
  • @dotjoe, you was the first to point out my mistake so if you want to create an answer I will grant it. – Banshee Apr 14 '11 at 07:57
  • that's cool, you can just give it to @qes because that's a pretty good answer. – dotjoe Apr 14 '11 at 14:36

1 Answers1

3

This is a well-known issue with HTML forms.

If you have a checkbox input, and it is never checked, the value is not posted.

Html.CheckBox & Html.CheckBoxFor render an additional hidden element with the same name to ensure that some value always gets posted. I believe Ruby on Rails uses the same technique (and possibly others).

There's a short discussion on the Asp.Net forum about this, as well as some previous StackOverflow questions.

Community
  • 1
  • 1
quentin-starin
  • 26,121
  • 7
  • 68
  • 86