I have a problem that’s starting to drive me crazy.. I’ve narrowed my problem down to the following test case:
Model:
public class TestModel
{
public bool TestBool { get; set; }
}
Index View:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.Action("Test")
Test View:
@model IEnumerable<Test_GUI.Models.TestModel>
@using (Html.BeginForm()) {
<table>
@Html.EditorForModel()
</table>
<input type="submit" value="OK"/>
}
Editor template;
<tr>
<td>
Yes
@Html.RadioButtonFor(m => m.TestBool, Model.TestBool)
</td>
<td>
No
@Html.RadioButtonFor(m => m.TestBool, !Model.TestBool)
</td>
In the TestController, I create two instances of TestModel with a value of false and pass them to the view. But the radiobuttons are rendered as checked, and they also return as true if I post the form..
I’ve tried many other ways to display the radiobuttons, but nothing seems to work. This seems like to correct way to do it.
I must be able use the current value of the boolean, so I cannot use fixed true or false values in the view. If I do use a fixed true/false, I do get the correct values on the form and in the controller after posting the form..
Try like this: