How do I set a checkbox in razor view?

后端 未结 10 2076
南笙
南笙 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:54

    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 讨论(0)
  • 2020-12-05 17:59

    you set AllowRating property to true from your controller or model

          @Html.CheckBoxFor(m => m.AllowRating, new { @checked =Model.AllowRating })
    
    0 讨论(0)
  • 2020-12-05 18:00

    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.

    0 讨论(0)
  • 2020-12-05 18:00

    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

    0 讨论(0)
提交回复
热议问题