Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

There are three groups of radio buttons on my page and I want to submit each of their values with one button.

<table>
    <tr>
     <td>
      @Html.RadioButton("rating1", "yes", true) Yes
      @Html.RadioButton("rating1", "no", false) No
      @Html.RadioButton("rating1", "maybe", false) Maybe
     </td>
    </tr>
<tr>
     <td>
      @Html.RadioButton("rating2", "yes", true) Yes
      @Html.RadioButton("rating2", "no", false) No
      @Html.RadioButton("rating2", "maybe", false) Maybe
     </td>
    </tr>
<tr>
     <td>
      @Html.RadioButton("rating3", "yes", true) Yes
      @Html.RadioButton("rating3", "no", false) No
      @Html.RadioButton("rating3", "maybe", false) Maybe
     </td>
    </tr>
</table>

I want to send a dictionary as a parameter to the controller action so I can retrieve the values for each rating.

The controller looks like this:

public ActionResult Rate(Guid uniqueId, Dictionary<Guid, string> ratings)
        {
            [...]
        }

I have tried:

<input type="submit" value="Send Ratings" onclick="@Url.Action("Rate", "Controller", new {new Dictionary<string,string> {{"rating1", "yes"}, {"rating2", "no"}, {"rating3", "maybe"}})"></input>

but passing a Dictionary as a RouteValue like that is not allowed.

How do I send all 3 radio button [name,value] pairs to the action with one button /submit? Also, should all three groups be in the same form or separate forms?

I am open to using javascript, but would prefer using Razor HTML helpers.

Thanks

share|improve this question
    
First, Razor code is parsed on the server before its sent to the view so @Url.Action renders a value before any client changes. Second you cant pass a dictionary to a GET method (at least not like that - it would need be something like in this answer). But really, why are you not just posting a form and binding to a model? – Stephen Muecke Feb 12 at 2:39

1 Answer 1

You can use FormCollection for gating your radio button value on your post.

you have to simply write var variable = f["rating1"]; in your post method and u get your selected radio button value on post method,

   public ActionResult Rate(Guid uniqueId,FormCollection f)

       {

           var variable1 = f["rating1"]; 

           var variable2 = f["rating2"]; 

           var variable3 = f["rating3"]; 

           [...]

       }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.