Set a RadioButtonFor() checked by default within a Foreach loop

前端 未结 2 496
旧巷少年郎
旧巷少年郎 2021-01-06 07:41

I have a weird behavior using @Html.RadioButtonFor Extension Method. I am using a foreach loop to create a list of RadioButton and By ternary operator. I am try

2条回答
  •  Happy的楠姐
    2021-01-06 08:12

    The precedence of operators is causing the problem here; as equality comes before comparison, the phrase will be evaluated as

    (@checked = item.Default) ? "checked" : ""
    

    whereas you want it to be

    @checked = (item.Default ? "checked" : "")
    

提交回复
热议问题