我想为我的单选按钮组进行模型绑定,情况如下: 每当我想要检查USD或%单选按钮时,在我的控制器中我希望在按下提交表单按钮时从所选单选按钮中获取真值或假值,但看起来View并没有传递给我任何值因为我在模型中为它们设置了int属性。除了我的单选按钮,我从表单中获取所有值。 我将不胜感激任何帮助
@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 :(得分:0)
要将字段绑定到模型,您需要使用&#34; for&#34;帮手。尝试将单选按钮更改为
@Html.RadioButtonFor(x => x.LstOrderDetails[i].OrderDetailID, "Dollar")
@Html.RadioButtonFor(x => x.LstOrderDetails[i].OrderDetailID, "Percent")
由于两者都绑定到同一个字段,因此该字段将具有所选单选按钮的值(本例中为美元或百分比)