i have created a list of model binder with checkbox that is supposed to handle my forms but when i post to controller it always set checkbox to false even if it is checked on client side, any help?
my model
public class RoleCheckbox
{
public int id { get; set; }
public bool ischecked { get; set; }
public string name { get; set; }
}
public class UsersNew
{
public IList<RoleCheckbox> Roles { get; set; }
[Required,MaxLength(128)]
public string username { get; set; }
[Required,DataType(DataType.Password)]
public string password { get; set; }
[Required,MaxLength(256),DataType(DataType.EmailAddress)]
public string email { get; set; }
}
client code
<div class="panel panel-default">
<div class="panel-heading">Roles</div>
<div class="panel-body">
<ul class="list-group">
@for (var i = 0; i < Model.Roles.Count; i++)
{
<li class="list-group-item">
@Html.Hidden("Roles[" +i +"].id",Model.Roles[i].id)
<label for="Roles_@(i)_ischecked">
@Html.CheckBox("Roles[" +i+ "].id",Model.Roles[i].ischecked)
@Model.Roles[i].name
</label>
</li>
}
</ul>
</div>
</div>
my method
HttpPost,ValidateAntiForgeryToken]
public ActionResult New(UsersNew form)
{
var user = new User();
syncRole(form.Roles, user.Roles);
if (Database.Session.Query<User>().Any(u => u.username == form.username))
ModelState.AddModelError("username", "username must be unique");
if (!ModelState.IsValid)
return View(form);
user.email = form.email;
user.username = form.username;
user.SetPassword(form.password);
Database.Session.Save(user);
return RedirectToAction("index");
}
idproperty really aboolean?)