48

I need to check a checkbox by default:

I tried all of these, nothing is checking my checkbox -

@Html.CheckBoxFor(m => m.AllowRating, new { @value = "true" })

@Html.CheckBoxFor(m => m.AllowRating, new { @checked = "true" })

@Html.CheckBoxFor(m => m.AllowRating, new { @checked = true })

@Html.CheckBoxFor(m => m.AllowRating, new { @checked = "checked"})
kheya
  • 7,546
  • 20
  • 77
  • 109

10 Answers10

43

You should set the AllowRating property to true, preferably in the controller or model.
Like other inputs, the checkbox's state reflects the value of the property.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
20

This works for me:

<input id="AllowRating" type="checkbox" @(Model.AllowRating?"checked='checked'":"")    style="" onchange="" />

If you really wants to use HTML Helpers:

@Html.CheckBoxFor(m => m.AllowRating, new { @checked = Model.AllowRating})

Also take into account that if m.AllowRating is false, it will fail to set to status checked in your examples.

Carlos Toledo
  • 2,519
  • 23
  • 23
  • 4
    First example is incorrect. Anytime the checked attribute is present, even if it is set to false or an empty string, is will be true. – Ben Sep 24 '19 at 18:24
  • 3
    @Ben this would avoid that problem: `` – QFDev Sep 23 '20 at 19:54
17

The syntax in your last line is correct.

 @Html.CheckBoxFor(x => x.Test, new { @checked = "checked" })

That should definitely work. It is the correct syntax. If you have an existing model and AllowRating is set to true then MVC will add the checked attribute automatically. If AllowRating is set to false MVC won't add the attribute however if desired you can using the above syntax.

Mark
  • 21,067
  • 14
  • 53
  • 71
  • Somehow it is not preselecting the checkbox. This is what I have in model: public bool AllowComments { get; set; } – kheya May 27 '11 at 20:58
  • Using the new { @checked = "checked } syntax will behave a bit strange when your page has validation errors: When you uncheck it in the browser, submit the form and then get the same form again presented (with validation error messages visible), then the checkbox will be rendered checked, right? – TweeZz May 27 '11 at 21:02
8

You can do this with @Html.CheckBoxFor():

@Html.CheckBoxFor(m => m.AllowRating, new{@checked=true });

or you can also do this with a simple @Html.CheckBox():

@Html.CheckBox("AllowRating", true) ;
alexandre-rousseau
  • 2,321
  • 26
  • 33
Atul
  • 81
  • 1
  • 1
5

you set AllowRating property to true from your controller or model

      @Html.CheckBoxFor(m => m.AllowRating, new { @checked =Model.AllowRating })
BrainCoder
  • 5,197
  • 5
  • 30
  • 33
3
<input type="checkbox" @( Model.Checked == true ? "checked" : "" ) />
Ben
  • 1,853
  • 19
  • 20
2

only option is to set the value in the controller, If your view is Create then in the controller action add the empty model, and set the value like,

Public ActionResult Create()
{
UserRating ur = new UserRating();
ur.AllowRating = true;
return View(ur);
} 
Murthy M
  • 119
  • 1
  • 2
0

I did it using Razor , works for me

Razor Code

@Html.CheckBox("CashOnDelivery", CashOnDelivery) (This is a bit or bool value) Razor don't support nullable bool
@Html.CheckBox("OnlinePayment", OnlinePayment)

C# Code

 var CashOnDelivery = Convert.ToBoolean(Collection["CashOnDelivery"].Contains("true")?true:false);
 var OnlinePayment = Convert.ToBoolean(Collection["OnlinePayment"].Contains("true") ? true : false);
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
0

If we set "true" in model, It'll always true. But we want to set option value for my checkbox we can use this. Important in here is The name of checkbox "AllowRating", It's must name of var in model if not when we post the value not pass in Database. form of it:

@Html.CheckBox("NameOfVarInModel", true) ;

for you!

@Html.CheckBox("AllowRating", true) ;
0

I had the same issue, luckily I found the below code

@Html.CheckBoxFor(model => model.As, htmlAttributes: new { @checked = true} )

Check Box Checked By Default - Razor Solution

Community
  • 1
  • 1
Spider
  • 514
  • 1
  • 10
  • 22