mvc bind/post boolean to radiobutton

前端 未结 4 969
有刺的猬
有刺的猬 2021-02-07 20:29

I have a column in my Model with a NULLABLE boolean value. Now on my View (for editing), I would like to bind that to two radiobuttons: Yes & No. If the value is null, then

4条回答
  •  北海茫月
    2021-02-07 21:11

    This is a razor solution to the problem. This is a very old ASP question.

    The problem is that you need to set the checked attribute because the Html.RadioButtonFor does not check a radio button based on a nullable bool (which appears to be a flaw).

    Also by putting the radio buttons inside of the label tag, you can select value by clicking the label.

    Shared/EditorTemplates/Boolean.cshtml

    @model bool?
    
    
    
    

提交回复
热议问题