0

I have an MVC project that has a radio button. Im using a custom input so it looks a bit like this.

<input class="cb_yes" data-val="true" data-val-required="You must select" id="radio_Yes" name="RadioButton" type="radio" value="True">
<span id="radio-yes"></span>Yes</label>
<input class="cb_no" data-val="true" data-val-required="You must select" id="radio_No" name="RadioButton" type="radio" value="False">
<span id="radio-no"></span>No</label>

Notice that the name is the same.

When Yes is clicked, in the browser console I can do

$("input[name='RadioButton']").val() and it came back as True. So far so good.

I'm then doing some C# to convert this value to a c# bool.

I'm doing

IsAlreadyCustomer = Convert.ToBoolean(controllerContext.HttpContext.Request.Form["RadioButton"] == "True");

but it's coming back as false. I'm looking in the "QuickWatch" to test and doing

controllerContext.HttpContext.Request.Form["RadioButton"] == "True" but thats, no suprise, false.

I cant seem to see why it's coming back as false. I dont get why my browser says True but QuickWatch is saying false

Have I wrote it correctly? What am I doing wrong?


Update:

First, I'm declaring a bool, then I try to set the bool to true based on the value of the radio button.

bool IsAlreadyCustomer;
   //if null then false, else         
 IsAlreadyCustomer = Convert.ToBoolean(controllerContext.HttpContext.Request.Form["RadioButton"] == "True");`
        `
AskYous
  • 4,332
  • 9
  • 46
  • 82
user2903379
  • 375
  • 3
  • 22
  • Have you tried checking `Form["RadioButton"]` to see what the value is? – itsme86 Aug 08 '18 at 21:45
  • yea. "Form doesn't exist in the current context" – user2903379 Aug 08 '18 at 21:48
  • I meant with all the preceding junk too. Just examine the value of what you're trying to compare to "True". – itsme86 Aug 08 '18 at 21:49
  • As long as the submitted form data has `RadioButton: True` in the body, your code should generate `true` for the `IsAlreadyCustomer` variable. – Shyju Aug 08 '18 at 21:49
  • BTW, You do not need the `Convert.ToBoolean` call as the `==` expression will return a boolean value. – Shyju Aug 08 '18 at 21:49
  • @itsme86 yes I did just `controllerContext.HttpContext.Request.Form["RadioButton"] == "True"` and it came back `"False"` – user2903379 Aug 08 '18 at 21:51
  • No, I mean examine the value. You're examining the comparison result. What does `controllerContext.HttpContext.Request.Form["RadioButton"]` evaluate to? ` == "True"` is false, but what's the ? – itsme86 Aug 08 '18 at 21:53
  • In my code, I'm setting IsAreadyCustomer to the value of the radio button. Ive declared `IsAlreadyCustomer` as a bool. Then setting it using the code above. It should be coming back as True but its being set to false. I'll update my question for more info – user2903379 Aug 08 '18 at 21:56
  • 1
    You're not setting `IsAlreadyCustomer` to the value of the radio button though. You're setting it to the result of comparing the value of the radio button to "True". – itsme86 Aug 08 '18 at 22:00
  • Put a breakpoint in the method. Run to the breakpoint. Go to the `Immediate Window` and evaluate `?controllerContext.HttpContext.Request.Form["RadioButton"]` What **exactly** does it display? – mjwills Aug 08 '18 at 22:01
  • @mjwills it comes back as `"False"` (as a string) – user2903379 Aug 08 '18 at 22:03
  • 5
    It would be awesome if you could provide a [mcve]. – mjwills Aug 08 '18 at 22:04
  • 3
    Instead of doing it all in the hardest way possible, you should (1) create and use a **Model class**, (2) use **HTML Helpers** for rendering the HTML output, and (3) use **Model Binding** instead of messing around with Request.Form. For examples of how to do that, see e.g. **[this question](https://stackoverflow.com/q/2559208/1220550)**. – Peter B Aug 08 '18 at 23:02
  • @PeterB Unfortunatley that option isnt available for me – user2903379 Aug 09 '18 at 08:13

0 Answers0