'Displaying radio buttons using ASP.NET MVC EditorForModel

I am using EditorForModel bound to a ViewModel. I have to display a list of radio buttons on my form, but I'm not sure how to do this using Data Annotations. Is this possible? Or can I not use EditorForModel in this case?

My ViewModel can be flexible, as it's only a DTO and I am manually mapping it to my data access layer.



Solution 1:[1]

Create a custom editor template:

[UIHint("RenderAsRadioButtonsList")]
public SomeObject ToBeRenderedAsRadioButtons { get; set; }

and then, in Views/Shared/EditorTemplates, create the partial view:

Views
    /Shared
        /EditorTemplates
            RenderAsRadioButtonsList.cshtml

Solution 2:[2]

See this Geekswithblogs post on a radio button helper.

Also another one.

A google search will turn up additional resources for radio button lists in ASP.NET MVC.

Here is some code over at GitHub, too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danludwig
Solution 2 Peter Mortensen