If I can throw my hat into the ring, I think there is a cleaner way than the existing answers to reuse the radio button functionality.
Let's say you have the following property in your ViewModel:
Public Class ViewModel <Display(Name:="Do you like Cats?")> Public Property LikesCats As Boolean End Class
You can expose that property through a reusable editor template:
First, create the file Views/Shared/EditorTemplates/YesNoRadio.vbhtml
Then add the following code to YesNoRadio.vbhtml:
@ModelType Boolean? <fieldset> <legend> @Html.LabelFor(Function(model) model) </legend> <label> @Html.RadioButtonFor(Function(model) model, True) Yes </label> <label> @Html.RadioButtonFor(Function(model) model, False) No </label> </fieldset>
You can call the editor for the property by manually specifying the template name in your View:
@Html.EditorFor(Function(model) model.LikesCats, "YesNoRadio")
Pros:
- Get to write HTML in an HTML editor instead of appending strings in code behind.
- Preserves the DisplayName DataAnnotation
- Allows clicks on Label to toggle radio button
- Least possible code to maintain in form (1 line). If something is wrong with the way it is rending, take it up with the template.