C# 在编辑视图ASP.NETMVC中显示选中单选按钮的适当方式是什么

C# 在编辑视图ASP.NETMVC中显示选中单选按钮的适当方式是什么,c#,asp.net-mvc,radio-button,html-helper,C#,Asp.net Mvc,Radio Button,Html Helper,联机、远程和物理数据的值,单位为DB,数据类型为int。在“编辑”视图中,我想选中以db为单位的值为1的单选按钮。在模型中,指定猫是随机的。我在编辑视图中使用了这个长if-else条件。如何改进我的代码,或者是否有其他方法来编写此代码: @if (Model.ApOnline == 1) { <span> Online: @Html.RadioButtonFor(x => x.AppointCat, "Online"

联机、远程和物理数据的值,单位为DB,数据类型为int。在“编辑”视图中,我想选中以db为单位的值为1的单选按钮。在模型中,指定猫是随机的。我在编辑视图中使用了这个长if-else条件。如何改进我的代码,或者是否有其他方法来编写此代码:

   @if (Model.ApOnline == 1)

     {
      <span>
      Online: @Html.RadioButtonFor(x => x.AppointCat, "Online", new { @checked = true })
                                        &nbsp; &nbsp;
       </span>
       <span>
       TeleMed: @Html.RadioButtonFor(x => x.AppointCat, "TeleMed")
                                        &nbsp; &nbsp;
       </span>
       <span>
       Physical: @Html.RadioButtonFor(x => x.AppointCat, "Physical")
       </span>

       }
       else if (Model.ApTele == 1)
       {
       <span>
       Online: @Html.RadioButtonFor(x => x.AppointCat, "Online")
            &nbsp; &nbsp;
       </span>
       <span>
       TeleMed: @Html.RadioButtonFor(x => x.AppointCat, "TeleMed", new { @checked = true })
                                   &nbsp; &nbsp;
        </span>
        <span>
                                        Physical: @Html.RadioButtonFor(x => x.AppointCat, "Physical")
                                    </span>
                                }
                                else
                                {
                                    <span>
                                        Online: @Html.RadioButtonFor(x => x.AppointCat, "Online")
                                        &nbsp; &nbsp;
                                    </span>
                                    <span>
                                        TeleMed: @Html.RadioButtonFor(x => x.AppointCat, "TeleMed")
                                        &nbsp; &nbsp;
                                    </span>
                                    <span>
                                        Physical: @Html.RadioButtonFor(x => x.AppointCat, "Physical", new { @checked = true })
                                    </span>
                                }
@if(Model.ApOnline==1)
{
联机:@Html.RadioButtonFor(x=>x.AppointCat,“联机”,新的{@checked=true})
远程通讯:@Html.RadioButtonFor(x=>x.AppointCat,“远程通讯”)
物理:@Html.radiobutton(x=>x.AppointCat,“物理”)
}
否则如果(Model.ApTele==1)
{
在线:@Html.RadioButtonFor(x=>x.AppointCat,“在线”)
远程通讯:@Html.radiobutton(x=>x.AppointCat,“远程通讯”,新的{@checked=true})
物理:@Html.radiobutton(x=>x.AppointCat,“物理”)
}
其他的
{
在线:@Html.RadioButtonFor(x=>x.AppointCat,“在线”)
远程通讯:@Html.RadioButtonFor(x=>x.AppointCat,“远程通讯”)
物理:@Html.RadioButtonOn(x=>x.AppointCat,“物理”,新的{@checked=true})
}
您可以从我的代码中了解到,我是MVC中的noob。
提前感谢您的帮助

您不需要
if/else
。此
Html.radiobutton(x=>x.AppointCat,“联机”)
表示为
AppointCat
创建单选按钮,如果
AppointCat
的值为
Online
,则选中此按钮。因此,您需要为
AppointCat
可以拥有的每个值创建一个。谢谢您的建议,但我将条件设置为这样。在线:@Html.RadioButtonFor(x=>x.AppointCat,“在线”,(Model.ApOnline==1)?新建{@checked=true}:null)这很好,但它是多余的。这就足够了:
Html.RadioButtonFor(x=>x.AppointCat,“Online”)
并为您完成了。我使用了它,但它不起作用