CheckboxFor is always null

431 Views Asked by At

i have a class ViewModel below:

public class UserViewModel{
  public bool? IsActive { get; set; }
}

View

@model CoreHRM.WebUI.Models.UserViewModel
@using (Html.BeginForm())
{
   @Html.CheckBoxFor(model => model.IsActive.Value)
   <button type="submit" class="btn btn-primary">Add New</button>
}

Controller

var isActive = userModel.IsActive;

but it's always get null value if i checked my checkbox.

help me to solve this problem?

2

There are 2 best solutions below

0
On

Rather than

@Html.CheckBoxFor(model => model.IsActive.Value)

Try

@Html.CheckBoxFor(model => model.IsActive)

I'm not sure you supply the value like that for a CheckBoxFor - you supply the model property

0
On

This solved my issue:

@Html.CheckBoxFor(model => model.IsPublic.Value,
    new { @Id = "IsPublic",@Name="IsPublic" })