Pregunta

I have a list of radio buttons with id, name and value attributes each.

in If(IsPost) to retrieve the selected radio button I have Request.Form[radio button name] which gives the value of the radio button. What if I want beside the value, the id of the button? What should? PS: I am using webmatrix to build the application

¿Fue útil?

Solución

In my opinion your best option is to use a value that gives you more informations, something like:

@{
    if (IsPost)
    {
        for (var i = 0; i < Request.Form.Count; i++)
        {
            string[] result = Request.Form[i].Split(',');
            <p>Group: @result[0] - Selection: @result[1]</p>
        }
    }
}

<html lang="en">
    <body>
        <form method="post">
            <h2>Group A</h2>
            <input type="radio" name="groupA" id="groupA" value="A,1">1<br>
            <input type="radio" name="groupA" id="groupA" value="A,2">2<br>
            <input type="radio" name="groupA" id="groupA" value="A,3">3<br>
            <input type="radio" name="groupA" id="groupA" value="A,4">4<br>
            <h2>Group B</h2>
            <input type="radio" name="groupB" id="groupB" value="B,1">1<br>
            <input type="radio" name="groupB" id="groupB" value="B,2">2<br>
            <input type="radio" name="groupB" id="groupB" value="B,3">3<br>
            <input type="radio" name="groupB" id="groupB" value="B,4">4<br>
            <input type="submit" />
        </form>
    </body>
</html>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top