How do I set a checkbox in razor view?

后端 未结 10 2106
南笙
南笙 2020-12-05 17:01

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          


        
10条回答
  •  醉梦人生
    2020-12-05 17:46

    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.

提交回复
热议问题