首页 > 解决方案 > 如何使用文本框值在单选按钮上设置值

问题描述

我的模型中有一个属性,它定义了用户要插入的数量。

为此,我使用 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>

问题是我在文本框中插入的值没有被传递给控制器​​,只有另一个字符串。

我需要能够覆盖视图中的值或能够在控制器的文本框中获取值。

有什么帮助吗?谢谢你。

标签: c#asp.net-mvc

解决方案


您需要为将为其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; }
}

现在在控制器中,您需要检查是否选择了其他单选按钮,使用文本框中提供的选定值。


推荐阅读