Reputation: 68
I have in my model a property that defines the quantity to be inserted by the user.
To do that I use 3 radio buttons like this:
The code I use for this is the following:
<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>
The problem is that the value that I insert in the textbox isn't being passed to the controller, only the other string.
I need to be able to override the value in the view or to be able to get the value in the textbox in the controller.
Any help? Thank you.
Upvotes: 0
Views: 262
Reputation:
Your view model needs a 2nd property to bind the textbox to. The DefaultModelBinder
only binds the first matching name/value pair, so it sees LabelsQuantity=Other
in the request, and sets that to your LabelsQuantity
proeprty, and ignores the LabelsQuantity=ValueFormTheTextBox
.
Change the model to include an additional property, say
public string OtherQuantity { get; set; }
and bind to it in the view using
@Html.TextBoxFor(m => m.OtherQuantity)
You should also consider applying a conditional validation attribute, such as a foolproof [RequiredIf("LabelsQuantity", "Other"]
attribute so that you get client and server side validation
Note also that if the value of the textbox should always be a number, then you should make the property int
rather than string
.
Upvotes: 1
Reputation: 62488
You need to introduce another property for the Others
text box that would post the value for it:
<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>
and in the model:
public class ViewModel
{
public string LabelsQuantity { get; set; }
...............
...............
public string Others { get; set; }
}
and now in controller you would need to check if the radio button others is selected use the selected value provided in text box.
Upvotes: 1