Make a checkbox checked or unchecked based on the value?

前端 未结 8 1301
星月不相逢
星月不相逢 2020-12-29 23:02

How can we make a checkbox checked or unchecked programatically based on the value? That is to say, for a particular user if the value is true, the checkbox should be checke

8条回答
  •  温柔的废话
    2020-12-29 23:26

    If you're using MVC and passing in model values correctly from your controller, then

    @Html.CheckBoxFor(model => model.checkBox1)
    

    ...is all you need. The html-helper does the logic to figure out whether or not to insert the checked="checkbox" code.

    Otherwise, without the HTML-helper you can dynamically generate the attribute yourself (others have pointed out how), but don't make the mistake of thinking that checked = "" will leave the box unchecked. See this answer for an explanation.

提交回复
热议问题