How do I set a checkbox in razor view?

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

    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);
    

提交回复
热议问题