0

In Cshtml file I have created a checkbox and using that true or false value of check box, need to write the logic in controller

In cshtml file

@Html.Checkboxfor(m=>m.isChecked,new {id="isChecked"}) @Html.Label("Ischecked",new {id="lblIsChecked", @class = "control-label"

In Model.cs

public bool isChecked {get;set;}

In controller

Model model=new Model();

If(model.isChecked == true) {

//Logic to write }

While checking isCheked variable in controller, I'm getting only false. Clarify this.

Jack
  • 1
  • 1
  • Does this answer your question? [ASP.NET MVC checkbox always false](https://stackoverflow.com/questions/44134973/asp-net-mvc-checkbox-always-false) – Anna Gevel Dec 08 '22 at 23:06

1 Answers1

0

Below is a work demo, you can refer to it:

In Controller:

       public IActionResult Index()
        {
            Model model = new Model();
            return View(model);
        }
        [HttpPost]
        public IActionResult Index(Model model)
        {          
            if (model.isChecked == true)
            {
                //Logic to write
            }
            return View();
        }
        

view:

@model Model
@using (Html.BeginForm())
{
@Html.CheckBoxFor(m => m.isChecked)
@Html.Label("ischecked","isChecked",new {id="lblIsChecked", @class = "control-label"})

<br/>
<input type="submit" value="submit" />
}

result:

enter image description here

Qing Guo
  • 6,041
  • 1
  • 2
  • 10