Are you sure you want a radio button? Seems like you might be after a Checkbox...
For a boolean, I would set up a 2 radiobuttons like so:
@Html.RadioButtonFor(m => m.Content, true, new { @id = "rb1" })
@Html.RadioButtonFor(m => m.Content, false, new { @id = "rb2" })
The html helper will figure out which one to check. If Model.Content
is of another type, use different values for the radiobuttons and MVC will still figure out which one to check.
Edit:
Actually, it's even simpler than that. If the you use the Html helper RadioButtonFor
method, it will check the radiobutton if it finds a value that matches the Model property. So you've specified your RadioButton as always having the same value as your Model property so it will always be checked (and that is the value that will be posted back in the form)
So, if you wanted a bool to default to false, and only return true if checked you could do just this:
@Html.RadioButtonFor(m => m.Content, true, new { @id = "rb1" })
where Content
is originally set to false.
You can do it with other value types as well. e.g. if Content was a string, initially set to null, this would post back "RadioButtonChecked"
only if the radio button was checked. Null otherwise:
@Html.RadioButtonFor(m => m.Content, "RadioButtonChecked", new { @id = "rb1" })
true
then I'm pretty sure that that will override the@checked = "false"
– Hortensiahorter