Question

How do you bind 2 radio buttons to a single model property in MVC using @Html.RadioButtonFor()

Was it helpful?

Solution

In your model:

public class MyModel
{
    public bool? DoYouAgree { get; set; }
}

In your view:

<label>
    @Html.RadioButtonFor(m => m.DoYouAgree, false, 
        new { id = "DoYouAgree_false" })
    No
</label>
<label>
    @Html.RadioButtonFor(m => m.DoYouAgree, true, 
        new { id = "DoYouAgree_true" })
    Yes
</label>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top