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
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) ;
you set AllowRating property to true from your controller or model
@Html.CheckBoxFor(m => m.AllowRating, new { @checked =Model.AllowRating })
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.
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