How can I use a textbox value to set a value on a radiobutton

jcsmata

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:

enter image description here .

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.

user3559349

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i use the value from a textbox component in blazor?

How can i use a symbol from a value of a radiobutton to calculate with two numbers?

How can i check a value is pasted in Textbox?

How can i get value from textbox?

How can I define a property that returns the current value of a RadioButton in Kotlin?

How can i add to value not set value

create a textbox based on the value of a radioButton

In Go, how can I use reflect to set the value of a map on a struct?

How can I use gson to create a set of key value pairs?

How can I use a condition to set a variable value in Ansible?

How to dynamicly set value radiobutton with JS in HTML

How can I set the Text of a textbox in Unity to a double return value of a script?

how to set textbox value in jquery

How can I use input textbox value in the ng-template script tag of same angularjs page

C# ComboBox and TextBox dynamic value - How can I access a TextBox value from another function or event?

How do I get the value/input of a textbox to use in js?

How can I convert textbox value to double in c#?

How can I add value to existing textbox in xaml

How can I sum multiple Numeric textbox value?

Javascript - How can I send the value of the text area to my textbox

How can I get button value in textbox of model pop up?

How can I get textbox value into array and convert into integer in java

How can i get the textbox value in selected row in gridview

How can I reference the value of a textbox in a UserForm in VBA?

how to automatically set value of textbox according to value of another textbox

How to set a TextBox value when another TextBox value is changed

How can I set value to an HTML attribute?

How can i set value to dropdown in flutter?

How I can set the value of element of a fragment?