C# RadioButton用于在多个选项中仅选择一个选项

C# RadioButton用于在多个选项中仅选择一个选项,c#,asp.net-mvc,radio-button,C#,Asp.net Mvc,Radio Button,我有一个包含两个布尔值的模型: public bool m_IsOptionA {get;set;} public bool m_IsOptionB {get;set;} 在我看来,我想生成两个单选按钮,用户必须选择其中一个。所以,基本上,我需要这样做,如果一个被选中,另一个被取消选中 在我的控制器中,在将模型传递给视图之前,我对对象列表中的每一项初始化这些值,如下所示: foreach (var item in m_ListObjs) { item.m_IsOptionA = tru

我有一个包含两个布尔值的模型:

public bool m_IsOptionA {get;set;}
public bool m_IsOptionB {get;set;}
在我看来,我想生成两个单选按钮,用户必须选择其中一个。所以,基本上,我需要这样做,如果一个被选中,另一个被取消选中

在我的控制器中,在将模型传递给视图之前,我对对象列表中的每一项初始化这些值,如下所示:

foreach (var item in m_ListObjs)
{
    item.m_IsOptionA = true;
    item.m_IsOptionB = false;
}
然后,在我看来,我为单选按钮生成以下代码:

@for (int i = 0; i < Model.Count; i++)
    {
        <p>
            <tr>
                <td>@Html.RadioButtonFor(x => x[i].m_IsOptionA, new{id = "m_IsAuction_true"}, new {style = "width:100px"})</td>
                <td>@Html.RadioButtonFor(x => x[i].m_IsOptionB, new{id = "m_IsBuyItNow_false"}, new {style = "width:130px"})</td>
            </tr>
        </p>
    }
@for(int i=0;ix[i].m_IsOptionA,新的{id=“m_IsAuction\u true”},新的{style=“width:100px”})
@RadioButton(x=>x[i].m_IsOptionB,新的{id=“m_IsBuyItNow\u false”},新的{style=“width:130px”})

}
但它们都可以被检查,并且在检查后保持检查状态,这不是我想要的行为

我需要一些javascript吗?是的,我怎么能这样做,如果不是,我做错了什么


谢谢大家!

好的!多亏了@Digger37,我明白了我的错误。我不需要有2个布尔值,而是需要为每个值设置一个和两个单选按钮

这是我的布尔值:

public bool m_IsOptionSelected {get;set;}
下面是我的新观点:

<td>@Html.RadioButtonFor(x => x[i].m_IsOptionSelected , "true", new {style = "width:100px"}) Option A</td>
<td>@Html.RadioButtonFor(x => x[i].m_IsOptionSelected , "false", new {style = "width:100px"}) Option B</td>
@Html.radiobutton(x=>x[i].m_IsOptionSelected,“true”,新的{style=“width:100px”})选项A
@RadioButton(x=>x[i].m_IsOptionSelected,“false”,新的{style=“width:100px”})选项B

而且它工作得很好。谢谢

为什么有单选按钮,当然复选框更好。如果选中,选项A为真,b为假。如果不检查,则相反。嗯,这也可能是一种选择。只是我从未使用过单选按钮,我想学习如何使用。而且,无论如何,对于这种行为,我还需要确保如果一个复选框被选中,另一个复选框被选中。-这可能会有帮助