【问题标题】:how to show and use Radio button in Asp.Net MVC3如何在 Asp.Net MVC3 中显示和使用单选按钮
【发布时间】:2012-01-23 13:02:49
【问题描述】:

我想在我的 Asp.Net MVC3 应用程序中有两个单选按钮。我的数据库中没有代表这些单选按钮的字段。 所以我不能这样使用它们

 @Html.LabelFor(model => model.Monthly)
 @Html.EditorFor(model => model.Yearly)

我试图用它来显示我的单选按钮

 @Html.Label("Monthly","Monthly")
 @Html.RadioButton("Monthly","Monthlyy")
 @Html.Label("Yearly","Yearly")
 @Html.RadioButton("Yearly","Yearly")

按钮是可见的,但用户可以同时选择这两个按钮。而我希望一次只能检查一个按钮。 此外,我还需要选中单选按钮的标签以实现我的应用程序的更多功能。如何添加单选按钮以及如何获取选中单选按钮的标签(文本值)。

请指导如何实现。

【问题讨论】:

    标签: asp.net asp.net-mvc-3


    【解决方案1】:

    为了能够对单选按钮进行分组,它们需要具有相同的名称。即

    <input type="radio" name="group1" value="No"/>
    <input type="radio" name="group1" value="Yes"/>
    <input type="radio" name="group2" value="No"/>
    <input type="radio" name="group2" value="Yes"/>
    

    所以在你的情况下,你想要做的是以下

    @Html.Label("Monthly")
    @Html.RadioButton("MonthlyOrYearly", "Monthly")
    @Html.Label("Yearly")
    @Html.RadioButton("MonthlyOrYearly", "Yearly")
    

    【讨论】:

    • 好的,谢谢。以及如何获取选定(选中)单选按钮的标签(文本)?我想在我的控制器的变量中包含所选收音机的文本。 var radText=Checked Radio Button Text
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2018-02-02
    • 2019-02-23
    • 2014-09-26
    • 2012-07-12
    相关资源
    最近更新 更多