2016-12-12 22 views
0

RadioButtonForにクラスを適用して、ラジオボタンとラベルの間の幅があまり大きくならないようにします。クラスがなければ、ラジオボタンには以下のように左右に大きなスペースがあります。しかし、クラスを適用すると、現在選択されているラジオボタンだけが、if条件が使用されているため、空白が削除されます(下の「Tell You Later」オプションを参照)。どのようにクラスをすべてのラジオボタンに適用できますか? 次のコードを変更しようとしましたが、このメソッドがすべてのラジオボタンにクラスを適用したにもかかわらず、データベースに保存されているラジオボタンの選択がラジオボタンを修正するために適用されないという問題があります。以下の方法では、最後のラジオボタンが常に選択される:以下item.IsChecked == true ? new { @checked = "checked", @class = "myclassradio" } : new { @checked = "unchecked", @class = "myclassradio"}RadioButtonForにクラスを適用する

enter image description here

を私の完全なコード:

@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>  
} 

答えて

0

私はそれを考え出しました。 foreachループの内部にIF条件を置くだけです。

@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>  
    } 
} 
関連する問題