2014-07-09 160 views
1

我想爲我的單選按鈕組做模型綁定,情況是這樣的: 每當我想要檢查USD或%單選按鈕,在我的控制器中我想獲得當我按下提交表單按鈕時,所選單選按鈕的真值或假值,但似乎View並沒有傳遞任何值,因爲我在模型中爲它​​們設置了int屬性。除了我的單選按鈕外,我從表格中獲得所有值。 我希望得到任何幫助在Asp.Net MVC 4.0 RadioButtonList模型綁定

@for (int i = 0; i < Model.LstOrderDetails.Count; i++) 
    { 

     <tr> 
     @if (!string.IsNullOrEmpty(Model.LstOrderDetails[i].OrderedDiscountAmount) && 
     (!string.IsNullOrEmpty(Model.LstOrderDetails[i].OrderedDiscountPerc))) 
     { 
      if(decimal.Round(Convert.ToDecimal(Model.LstOrderDetails[i].OrderedDiscountAmount), 2, MidpointRounding.AwayFromZero) != 0M) 
     {           
      <td class="col-md-2 col-xs-2"> 
      <table style="font-size: 11px;"> 
       <tr> 
       <td> 
        USD 
       </td> 
       <td>                               @Html.RadioButton(Model.LstOrderDetails[i].OrderDetailID, Model.LstOrderDetails[i].IsDiscountAmnt, Convert.ToBoolean(Model.LstOrderDetails[i].IsDiscountAmnt)) 
       </td> 
       <td rowspan="2">@Html.TextBoxFor(x => x.LstOrderDetails[i].OrderedDiscountAmount, new { @class = "form-control", placeholder = "Discount", style = "font-size: 12px;" }) 
      </td> 
      </tr> 
      <tr> 
       <td> 
       % 
       </td> 
       <td>@Html.RadioButton(Model.LstOrderDetails[i].OrderDetailID,Model.LstOrderDetails[i].IsDiscountPerc, Convert.ToBoolean(Model.LstOrderDetails[i].IsDiscountPerc)) 
       </td> 
       <td> 
       </td> 
      </tr> 
      </table> 
     </td> 
     } 

     else 
     { 
     if (decimal.Round(Convert.ToDecimal(Model.LstOrderDetails[i].OrderedDiscountPerc), 2, MidpointRounding.AwayFromZero) != 0M) 
     { 
     removeafterzero = Model.LstOrderDetails[i].OrderedDiscountPerc.Substring(0, Model.LstOrderDetails[i].OrderedDiscountPerc.LastIndexOf('.')); 

     <td class="col-md-2 col-xs-2"> 
     <table style="font-size: 11px;"> 
     <tr> 
     <td> 
     USD 
     </td> 
     <td> @Html.RadioButton(Model.LstOrderDetails[i].OrderDetailID, Model.LstOrderDetails[i].IsDiscountAmnt, Convert.ToBoolean(Model.LstOrderDetails[i].IsDiscountAmnt)) 
     </td> 
     <td rowspan="2">@Html.TextBoxFor(x => removeafterzero, new { @class = "form-control", placeholder = "Discount", style = "font-size: 12px;" }) 
     </td> 
     </tr> 
     <tr> 
     <td> 
      % 
     </td> 
     <td> @Html.RadioButton(Model.LstOrderDetails[i].OrderDetailID, Model.LstOrderDetails[i].IsDiscountPerc, Convert.ToBoolean(Model.LstOrderDetails[i].IsDiscountPerc)) 
     </td> 
     <td> 
     </td> 
    </tr> 
    </table> 
    </td> 
    } 
} 
    } 

//Model Part 
public class OrderDetail 
    { 
     . 
     . 
     . 
     public string OrderedDiscountAmount { get; set; } 
     public string OrderedDiscountPerc { get; set; } 
     public int IsDiscountPerc { get; set; } 
     public int IsDiscountAmnt { get; set; } 
    } 

回答

0

到現場配合你的模型,你需要使用一個「爲」幫手。試着改變你的單選按鈕

@Html.RadioButtonFor(x => x.LstOrderDetails[i].OrderDetailID, "Dollar") 
@Html.RadioButtonFor(x => x.LstOrderDetails[i].OrderDetailID, "Percent") 

,因爲二者都連接到相同的領域,該領域將有選擇的單選按鈕(在這個例子中美元或百分比)的值