1

How do I handle postback for radio buttons in ASP MVC 5? This tells how to do it for text inputs, but not radio buttons. With radio buttons, all radio buttons have the same names, so the model doesn't apply. Do I need a for loop of if statements, or is there a simple way?

For some example code:

    <ul class="dropdown-menu report-checkbox-dropdown" role="menu" aria-labelledby="menu1">
        <li><input type="radio" name="quantity" value="all_quantities" data-stoppropagation='true' />All items</li>
        <li><input type="radio" name="quantity" value="in_stock" data-stoppropagation='true' />In Stock</li>
        <li><input type="radio" name="quantity" value="out_of_stock" data-stoppropagation='true' />Out of Stock</li>
    </ul>

While I'm at it, I'll ask about how to do it with checkboxes -- probably the same.

Edit I tried sticking in @Request.Form.AllKeys just to see what it would return, but all I got was System.String[].

abalter
  • 9,663
  • 17
  • 90
  • 145
  • Are you trying to keep the radio button selected after post back? Why not use a Viewbag variable that holds the information on page load? – code Jun 27 '15 at 23:50
  • What do your mean _"so the model doesn't apply"_? Of course it does. You creating a radio button group with `name="quantity"` which means it will bind to a property named `Quantity` –  Jun 28 '15 at 00:18
  • @StephenMuecke If I set the value of "quantity" to be checked, how would it know which button to check? They all have name "quantity". This is contrary to an text input for which there is a one-to-one correspondence with the name and the control. – abalter Jun 28 '15 at 01:12
  • @code yes, that is a solution. I learned that one should try to avoid using viewbag. Maybe that is overly strict. It seems like there ought to be a more righteous way. Also, in my current instance, I have a list of checkboxes created on the fly from data drawn from the database using ajax. In other words, the javascript creates a listbox for each record in a certain table. It seems like using a viewbag would be a little heavy handed for that, as I would need to fill the viewbag by repeating a query in the controller I have already done with my javascript. But maybe that's not such a big deal. – abalter Jun 28 '15 at 01:14
  • @abalter, You have 3 radio buttons with the values "all_quantities", "in_stock", and "out_of_stock". If you have a property `Quantity` then when you post, `Quantity` will be one of those values depending on which button is selected. But why are you not using `@Html.RadioButtonFor(m => m.Quantity, "all_quantities")` etc. so you get 2-way model binding (and all the other features of using the html helpers). If the value of `Quantity` is (say) "in_stock", then the second radio button will be selected when you render the view. –  Jun 28 '15 at 01:23
  • Hmmm. That's interesting. I should learn more about html helpers (although, mvc 6 is going to tag helpers--I don't know if they will operate the same way). However, I don't have a model with field quantity. It is a filter. You select a radio button, and press a submit in the view. The controller runs a query that select only items that are in stock, not in stock, or both. See what I mean? The name only comes from the form, not an entity. – abalter Jun 28 '15 at 02:22
  • Then do it the correct way - use a view model! - see [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jun 28 '15 at 03:16
  • Although if this is the only field your using to filter result, then it should be a GET call and you method parameter can be `(string quantity)` - the parameter will contain the selected value. –  Jun 28 '15 at 03:21

1 Answers1

0

Why don't you use RadioButtonFor helpers, which are explained here: Conditionally include checked attribute in html.RadioButtonFor (MVC4/Razor).

This answer may help too: MVC RadioButtonFor How & What to POST back to Controller

Community
  • 1
  • 1
Marcin Zablocki
  • 10,171
  • 1
  • 37
  • 47
  • I *could* but there are some reasons not to. (1) MVC 6 is [moving to tag helpers](http://www.davepaquette.com/archive/2015/05/11/cleaner-forms-using-tag-helpers-in-mvc6.aspx), (2) the radio buttons don't apply to a particular field of a particular model -- they are used for filtering a table of results. I could create a view model, but that is just making a lot more work. (3) My checkboxes are dynamically generated using Javascript, and (4) I don't think it should matter, in the sense that I should still be able to use value=@Request.Form... – abalter Jun 27 '15 at 22:55
  • @abalter Did you put your
      inside
    ? If not, there is no way of catching input values in Request.Form object.
    – Marcin Zablocki Jun 28 '15 at 20:59
  • Yes, it is in a form. – abalter Jun 28 '15 at 21:25
  • @abalter And do you submit this particular form? – Marcin Zablocki Jun 30 '15 at 19:36