50

I am using CheckBox in my ASP.Net MVC project,

i want to set checkBox by default checked,

My CheckBox is

@Html.CheckBoxFor(model => model.As, new { @checked = "checked" })

but its not working,,,,

Avinash Singh
  • 2,697
  • 11
  • 44
  • 72

8 Answers8

81

In your controller action rendering the view you could set the As property of your model to true:

model.As = true;
return View(model);

and in your view simply:

@Html.CheckBoxFor(model => model.As);

Now since the As property of the model is set to true, the CheckBoxFor helper will generate a checked checkbox.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I want to checked without changing model.as value in Controller.. how to change value in view – Avinash Singh May 08 '13 at 09:12
  • 5
    The correct way to achieve that is to set the value in the controller. That's how the CheckBoxFor helper is designed to be used. If you don't want to follow the best practices you could always manually generate the checkbox in the view with a hardcoded `checked="checked"` attribute. – Darin Dimitrov May 08 '13 at 09:13
  • i tried with @Html.CheckBoxFor(model => model.As, new { @checked = "true" }) but its not working – Avinash Singh May 08 '13 at 09:14
  • Yes, it's not working because the helper doesn't support this attribute. It uses the value of the `As` property on your model which, as I already explained in my answer, should be set in your controller action. But I repeat once again, if you don't want to follow this recommended approach you should hardcode your checkbox manually: ``. – Darin Dimitrov May 08 '13 at 09:17
  • After changing model.as value in controller its affecting other view, that's why i want to set in that particular view, – Avinash Singh May 08 '13 at 09:20
53

Old question, but another "pure razor" answer would be:

@Html.CheckBoxFor(model => model.As, htmlAttributes: new { @checked = true} )
tonjo
  • 1,394
  • 1
  • 14
  • 27
  • 2
    This has no effect if the model value is "false" (the checkbox is rendered unchecked) and is not needed if the model value is "true," so what is the point of this code? – Tom Regan Nov 10 '17 at 20:45
  • 2
    What about when it's null? (`bool ?`) – tonjo Nov 11 '17 at 21:48
  • 2
    It's indeed useful on Create New view. If you want to create or add new data, sometimes you want a default value. For example field for "Marital Status" - maybe you want to set "Yes" or "Checked" as default value on the form. – Cheries Mewengkang Sep 17 '18 at 09:01
3

You could set your property in the model's constructor

public YourModel()
{
    As = true;
}
Anonymous
  • 31
  • 1
3
@Html.CheckBox("yourId", true, new { value = Model.Ischecked })

This will certainly work

0

An alternative solution is using jQuery:

    <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            PrepareCheckbox();
        });
        function PrepareCheckbox(){
            document.getElementById("checkbox").checked = true;
        }
    </script>
Clement Hoang
  • 675
  • 9
  • 18
0

I use viewbag with the same variable name in the Controller. E.g if the variable is called "IsActive" and I want this to default to true on the "Create" form, on the Create Action I set the value ViewBag.IsActive = true;

public ActionResult Create()
{
    ViewBag.IsActive = true;
    return View();
}
dunwan
  • 1,577
  • 2
  • 16
  • 14
0

My way is @Html.CheckBoxFor(model => model.As, new { @value= "true" }) (meaning is checked)

Dzu Vu
  • 1
0

In razor, set value first.

@{model.As = true;}
@Html.CheckBoxFor(model => model.As)
kki
  • 11
  • 2