-1

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?

Ade Zaenudin
  • 31
  • 1
  • 11

2 Answers2

0

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

Neil Thompson
  • 6,356
  • 2
  • 30
  • 53
0

This solved my issue:

@Html.CheckBoxFor(model => model.IsPublic.Value,
    new { @Id = "IsPublic",@Name="IsPublic" })
David Medenjak
  • 33,993
  • 14
  • 106
  • 134