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

6
  • Why is it not possible to name it "RememberMe"? Are you using Html.CheckBox or just the input type="checkbox"? Commented Apr 11, 2011 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? Commented Apr 11, 2011 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. Commented Apr 11, 2011 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. Commented Apr 11, 2011 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. Commented Apr 14, 2011 at 7:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.