【问题标题】:Applying a class to RadioButtonFor将类应用于 RadioButtonFor
【发布时间】:2016-12-12 09:52:22
【问题描述】:

我正在为 RadioButtonFor 应用一个类,这样单选按钮和标签之间的宽度就不会太大。如果没有类,单选按钮的左右两侧有很大的空间,如下所示。但是,当我应用该类时,由于使用了 if 条件,只有当前选中的单选按钮会删除空格(请参见下图中的“稍后告诉您”选项)。如何将课程应用于所有单选按钮? 我尝试对代码进行以下更改,问题是即使此方法将类应用于所有单选按钮,保存在数据库中的单选按钮选择也不会应用于正确的单选按钮。使用以下方法,始终选中最后一个单选按钮:item.IsChecked == true ? new { @checked = "checked", @class = "myclassradio" } : new { @checked = "unchecked", @class = "myclassradio"}

我的完整代码如下:

@foreach (var item in Model.religion)
{                        
   <span>@Html.RadioButtonFor(m => m.SelectedReligion, item.ID, item.IsChecked == true ? new { @checked = "checked", @class = "myclassradio" } : null ) @item.MyReligion </span>    
}

【问题讨论】:

    标签: asp.net razor radiobuttonfor


    【解决方案1】:

    我想通了。只需在 foreach 循环中放置一个 IF 条件....duh?!!

    @foreach (var item in Model.religion)
        if ( item.IsChecked == true )  
        {                     
            <span>@Html.RadioButtonFor(m => m.SelectedReligion, item.ID, new { @checked = "checked", @class = "myclassradio" } ) @item.MyReligion </span>    
        }
        else
        {
            <span>@Html.RadioButtonFor(m => m.SelectedReligion, item.ID, new { @class = "myclassradio" } ) @item.MyReligion </span>    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 2016-06-23
      • 2014-01-17
      • 2015-11-01
      • 2012-11-22
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      相关资源
      最近更新 更多