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

From Dev

How can I set the value of textbox to "0" if blank or empty?

From Dev

How can I set the value of textbox to 0 if blank or empty?

From Dev

How can i save a value in the textbox to a variable

From Dev

How can i get value from textbox?

From Dev

How can i check a value is pasted in Textbox?

From Dev

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

From Dev

How can i add to value not set value

From Dev

How can I make a textBox value equal to a datagrivew cell value

From Dev

Set value on ASP:RadioButton

From Dev

how can I set a dataset or datatable row to a value taken from a gridview textbox entry?

From Dev

how to hide or show a textbox depending on radiobutton value in jsf

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Java

How can I sum multiple Numeric textbox value?

From Dev

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

From Dev

How can I add value to existing textbox in xaml

From Dev

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

From Dev

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

From Dev

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

From Dev

How can I set this value using ajax

From Dev

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

From Dev

How can I set default value in serializers?

From Dev

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

From Dev

How can I set the value of a variable?

From Dev

How to get radiobutton value?

From Dev

Set default textbox value that can't be removed

From Dev

Get the value of textbox when the radiobutton is checked and change the value of radiobutton based on textbox value

Related Related

  1. 1

    How can I set the value of textbox to "0" if blank or empty?

  2. 2

    How can I set the value of textbox to 0 if blank or empty?

  3. 3

    How can i save a value in the textbox to a variable

  4. 4

    How can i get value from textbox?

  5. 5

    How can i check a value is pasted in Textbox?

  6. 6

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

  7. 7

    How can i add to value not set value

  8. 8

    How can I make a textBox value equal to a datagrivew cell value

  9. 9

    Set value on ASP:RadioButton

  10. 10

    how can I set a dataset or datatable row to a value taken from a gridview textbox entry?

  11. 11

    how to hide or show a textbox depending on radiobutton value in jsf

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

    How can I sum multiple Numeric textbox value?

  17. 17

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

  18. 18

    How can I add value to existing textbox in xaml

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

    How can I set this value using ajax

  23. 23

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

  24. 24

    How can I set default value in serializers?

  25. 25

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

  26. 26

    How can I set the value of a variable?

  27. 27

    How to get radiobutton value?

  28. 28

    Set default textbox value that can't be removed

  29. 29

    Get the value of textbox when the radiobutton is checked and change the value of radiobutton based on textbox value

HotTag

Archive