Reputation: 6029
I have the follow class which I'm trying to wire up to the radio buttons
public class ProductFilter
{
public bool Tubs { get; set; }
public bool Packs { get; set; }
}
And I use this on the view as so
<div class="row col-lg-12" id="filters">
<div class="col-lg-4 text-right">
@Html.RadioButtonFor(model => model.Filter.Tubs, true) Tubs
</div>
<div class="col-lg-4"></div>
<div class="col-lg-4 text-left">
@Html.RadioButtonFor(model => model.Filter.Packs, true) Packs
</div>
</div>
Now when the page loads, and I select say tubs it posts back to the controller and I'm able to filter, when the page reloads and I select packs it posts back but the values for the both are the same, I would expect tubs to be false because I have selected packs and vice versa, can some one point out what I'm doing wrong?
Upvotes: 0
Views: 264
Reputation:
It appears you want to filter based on either a value of "Tubs" or "Packs", in which case you model is wrong. You need a property that posts back either of those strings, for example
public string Filter { get; set; }
public List<string> FilterList { get; set; }
and in the controller
model.FilterList = new List<string>() { "Tubs", "Packs" };
model.Filter = "Tubs"; // set default
and in the view
@foreach(var option in Model.FilterList)
{
<label>
@Html.DropDownListFor(m => m.Filter, option, new { id = option })
<span>@option</span>
</label>
}
which will generate 2 radio button with name="Filter"
and values of value="Tubs"
and value="Packs"
, and allow selection of only one.
Alternatively you could use an enum for the filter values.
Upvotes: 1