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

爱⌒轻易说出口 提交于 2019-12-02 12:59:18

问题


I have three radio buttons on my HTML form i.e.

<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?


回答1:


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.



来源:https://stackoverflow.com/questions/37420031/how-to-get-the-id-of-selected-radio-button-in-razor-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!