我的模型中有一个属性,它定义了用户要插入的数量。
为此,我使用了3个单选按钮,如下所示:
。
我使用的代码如下:
<div>
@Html.RadioButtonFor(m => m.LabelsQuantity, "100", new { @class = "custom-control-inline radiobtn" }) @Html.Label("100", new { @class = "custom-label-inline " })
</div>
<div>
@Html.RadioButtonFor(m => m.LabelsQuantity, "1000", new { @class = "custom-control-inline radiobtn" }) @Html.Label("1000", new { @class = "custom-label-inline " })
</div>
<div>
@Html.RadioButtonFor(m => m.LabelsQuantity, "Other" , new { @class = "custom-control-inline radiobtn" }) @Html.Label("Others", new { @class = "custom-label-inline " })
</div>
<div>
@Html.TextBoxFor(m => m.LabelsQuantity, string.Empty, new { @class = "custom-control-inline form-control enable_tb", type = "number", disabled = "true" })
</div>
问题是,我在文本框中插入的值并没有传递给控制器,而是传递给了另一个字符串。
我需要能够覆盖视图中的值,或者能够在控制器中获取文本框中的值。
有什么帮助吗?谢谢。
发布于 2018-08-16 21:23:46
视图模型需要第二个属性来绑定textbox。DefaultModelBinder
只绑定第一个匹配的名称/值对,因此它在请求中看到LabelsQuantity=Other
,并将其设置为您的LabelsQuantity
属性,并忽略LabelsQuantity=ValueFormTheTextBox
。
更改模型以包含一个附加属性,例如
public string OtherQuantity { get; set; }
并在视图中使用
@Html.TextBoxFor(m => m.OtherQuantity)
还应考虑应用条件验证属性,如foolproof [RequiredIf("LabelsQuantity", "Other"]
属性,以便获得客户端和服务器端验证
另请注意,如果textbox的值应始终为数字,则应将属性设置为int
而不是string
。
发布于 2018-08-16 21:23:18
您需要为Others
文本框引入另一个属性,该属性将发布该文本框的值:
<div>
@Html.RadioButtonFor(m => m.LabelsQuantity, "100", new { @class = "custom-control-inline radiobtn" }) @Html.Label("100", new { @class = "custom-label-inline " })
</div>
<div>
@Html.RadioButtonFor(m => m.LabelsQuantity, "1000", new { @class = "custom-control-inline radiobtn" }) @Html.Label("1000", new { @class = "custom-label-inline " })
</div>
<div>
@Html.RadioButtonFor(m => m.LabelsQuantity, "Other" , new { @class = "custom-control-inline radiobtn" }) @Html.Label("Others", new { @class = "custom-label-inline " })
</div>
<div>
@Html.TextBoxFor(m => m.Others, string.Empty, new { @class = "custom-control-inline form-control enable_tb", type = "number", disabled = "true" })
</div>
在模型中:
public class ViewModel
{
public string LabelsQuantity { get; set; }
...............
...............
public string Others { get; set; }
}
现在,在控制器中,您需要使用文本框中提供的选定值检查单选按钮others是否被选中。
https://stackoverflow.com/questions/51878026
复制相似问题