Use Html.RadioButtonFor and Html.LabelFor for the same Model but different values

前端 未结 7 1823
陌清茗
陌清茗 2020-12-13 02:11

I have this Razor Template

@Html.RadioButtonFor(i => i.Value, \"1\") @Html.LabelFor(i =>
相关标签:
7条回答
  • 2020-12-13 03:04

    Don't over-engineer a solution for this. All you are trying to accomplish is to have the radio buttons respond to clicks on the text. Keep it simple and just wrap your radio buttons in label tags:

    <table>
    <tr>
        <td><label>@Html.RadioButtonFor(i => i.Value, "1")True</label></td>
    </tr>
    <tr>
        <td><label>@Html.RadioButtonFor(i => i.Value, "0")False</label></td>
    </tr>
    </table>
    

    The LabelFor html helper is usually used to bring in the Name from the Display attribute on your View Model (e.g. "[Display(Name = "Enter your Name")]).

    With radio buttons, the name isn't particularly useful, because you have a different line of text for each radio button, meaning you are stuck hard coding the text into your view anyway.

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