简体   繁体   中英

How to get the id of selected radio button in Razor code?

I have three radio buttons on my HTML form ie

<input type="radio" name="radioBtn" id="rdBtn1" />
<input type="radio" name="radioBtn" id="rdBtn2" />
<input type="radio" name="radioBtn" id="rdBtn3" />

I want to see which radio button is selected so that I can further process. I am trying this

@functions{
    var choice = Request.Params["radioBtn"];
}

But the "choice" only returns 'on'. How can I know which one of these radio buttons are actually active at this point of time?

Currently your radio buttons aren't in any way different from one another, as far as the form is concerned. Give them values:

<input type="radio" name="radioBtn" id="rdBtn1" value="rdBtn1" />
<input type="radio" name="radioBtn" id="rdBtn2" value="rdBtn2" />
<input type="radio" name="radioBtn" id="rdBtn3" value="rdBtn3" />

That way the form can post more than just the binary option of whether or not the radio button was selected. Then choice should end up with the value of the one which was selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM