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"})
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:
for you!
The syntax in your last line is correct.
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.
This works for me:
If you really wants to use HTML Helpers:
I did it using Razor , works for me
Razor Code
C# Code
you set AllowRating property to true from your controller or model
I had the same issue, luckily I found the below code
Check Box Checked By Default - Razor Solution