dimanche 1 février 2015

Handling checkboxes in MVC controller

I have a html form to login, which contains amongst other inputs a checkbox for the "Remember me" function. For the other textboxes, I've been letting MVC match them up automatically (probably a word for that). So for textboxes I'd do "string [textboxname]" as a parameter, and that all works fine. I thought for a checkbox I could just do "bool [checkboxname]" but that does not work. Here is my form in the view:



@using (Html.BeginForm("login", "accounts"))
{
@Html.TextBox("email_username", null, new { @class = "textbox login email", placeholder = "Email or Username" });
@Html.TextBox("password", null, new { @class = "textbox login password", placeholder = "Password", type = "password" });
@Html.CheckBox("remember_me", new { @class = "checkbox remember-me" });
<input type="submit" value="Login" class="button login" />
}


And my controller action that handles the form submission:



[HttpPost]
public ActionResult Login(string email_username, string password, bool remember_me)
{
//Do things
}


What would be the best way to go about handling this?





Aucun commentaire:

Enregistrer un commentaire