In an MVC5 application, you can create a radio button list from an enum where each radio button has a label that displays the display name of the enum value. Here's how to do it:
Display attribute:public enum Color { [Display(Name = "Red")] Red, [Display(Name = "Green")] Green, [Display(Name = "Blue")] Blue } public class MyViewModel { public Color SelectedColor { get; set; } } EnumHelper class to get the enum values and their display names:@model MyViewModel @using System.Web.Mvc.Html @foreach (var value in Enum.GetValues(typeof(Color))) { var label = Enum.GetName(typeof(Color), value); var display = value.GetType() .GetMember(value.ToString())[0] .GetCustomAttributes(typeof(DisplayAttribute), false) .SingleOrDefault() as DisplayAttribute; if (display != null) { label = display.Name; } @Html.RadioButtonFor(m => m.SelectedColor, value, new { id = label }) @Html.Label(label, new { @for = label }) } In this example, we use a foreach loop to iterate over each enum value and create a radio button and label for it. We get the name of the enum value and its display name using the Enum.GetName method and the DisplayAttribute, respectively. If the enum value has a display name specified, we use that as the label text. Otherwise, we use the enum value name.
The RadioButtonFor method creates a radio button for the enum value and binds it to the SelectedColor property of the view model. The Label method creates a label for the radio button and sets its for attribute to match the radio button's id.
When the form is submitted, the selected value will be bound to the SelectedColor property of the view model.
"MVC5 Enum radio button example"
// Code Example: public enum Gender { [Display(Name = "Male")] Male, [Display(Name = "Female")] Female } // In View: @foreach (var gender in Enum.GetValues(typeof(Gender))) { <label> @Html.RadioButtonFor(model => model.SelectedGender, gender) @((Gender)gender).GetDisplayName() </label> } "MVC5 Enum radio button with DisplayName"
// Code Example: public enum Color { [Display(Name = "Red")] Red, [Display(Name = "Blue")] Blue, [Display(Name = "Green")] Green } // In View: @foreach (var color in Enum.GetValues(typeof(Color))) { <label> @Html.RadioButtonFor(model => model.SelectedColor, color) @((Color)color).GetDisplayName() </label> } "MVC5 Enum radio button DisplayName attribute"
// Code Example: public enum DayOfWeek { [Display(Name = "Monday")] Monday, [Display(Name = "Wednesday")] Wednesday, [Display(Name = "Friday")] Friday } // In View: @foreach (var day in Enum.GetValues(typeof(DayOfWeek))) { <label> @Html.RadioButtonFor(model => model.SelectedDay, day) @((DayOfWeek)day).GetDisplayName() </label> } "MVC5 Enum radio button group with DisplayName"
// Code Example: public enum Animal { [Display(Name = "Dog")] Dog, [Display(Name = "Cat")] Cat, [Display(Name = "Bird")] Bird } // In View: @foreach (var animal in Enum.GetValues(typeof(Animal))) { <label> @Html.RadioButtonFor(model => model.SelectedAnimal, animal) @((Animal)animal).GetDisplayName() </label> } "MVC5 Enum radio button display Enum values"
// Code Example: public enum Fruit { [Display(Name = "Apple")] Apple, [Display(Name = "Banana")] Banana, [Display(Name = "Orange")] Orange } // In View: @foreach (var fruit in Enum.GetValues(typeof(Fruit))) { <label> @Html.RadioButtonFor(model => model.SelectedFruit, fruit) @((Fruit)fruit).GetDisplayName() </label> } "MVC5 Enum radio button SelectList DisplayName"
// Code Example: public enum Language { [Display(Name = "English")] English, [Display(Name = "Spanish")] Spanish, [Display(Name = "French")] French } // In Controller: ViewBag.Languages = new SelectList(Enum.GetValues(typeof(Language)) .Cast<Language>() .Select(v => new SelectListItem { Text = v.GetDisplayName(), Value = v.ToString() })); // In View: @Html.DropDownListFor(model => model.SelectedLanguage, ViewBag.Languages as SelectList) "MVC5 Enum radio button SelectListItem DisplayName"
// Code Example: public enum Season { [Display(Name = "Spring")] Spring, [Display(Name = "Summer")] Summer, [Display(Name = "Autumn")] Autumn } // In Controller: ViewBag.Seasons = Enum.GetValues(typeof(Season)) .Cast<Season>() .Select(v => new SelectListItem { Text = v.GetDisplayName(), Value = v.ToString() }); // In View: @Html.DropDownListFor(model => model.SelectedSeason, ViewBag.Seasons as IEnumerable<SelectListItem>) "MVC5 Enum radio button with label DisplayName attribute"
// Code Example: public enum Country { [Display(Name = "United States")] UnitedStates, [Display(Name = "Canada")] Canada, [Display(Name = "United Kingdom")] UnitedKingdom } // In View: @foreach (var country in Enum.GetValues(typeof(Country))) { <label> @Html.RadioButtonFor(model => model.SelectedCountry, country) @((Country)country).GetDisplayName() </label> } "MVC5 Enum radio button with DisplayName in model"
// Code Example: public class CarViewModel { public CarType SelectedCarType { get; set; } } public enum CarType { [Display(Name = "Sedan")] Sedan, [Display(Name = "SUV")] SUV, [Display(Name = "Truck")] Truck } // In View: @foreach (var carType in Enum.GetValues(typeof(CarType))) { <label> @Html.RadioButtonFor(model => model.SelectedCarType, carType) @((CarType)carType).GetDisplayName() </label> } "MVC5 Enum radio button custom DisplayName function"
// Code Example: public enum MovieGenre { [Display(Name = "Action")] Action, [Display(Name = "Comedy")] Comedy, [Display(Name = "Drama")] Drama } // Extension Method: public static class EnumExtensions { public static string GetCustomDisplayName(this MovieGenre genre) { // Add custom logic here if needed return genre.GetDisplayName(); } } // In View: @foreach (var genre in Enum.GetValues(typeof(MovieGenre))) { <label> @Html.RadioButtonFor(model => model.SelectedGenre, genre) @((MovieGenre)genre).GetCustomDisplayName() </label> } touchablehighlight angular2-testing linefeed angular-template event-delegation azure-application-insights apache-spark-1.5 legend branch lazy-evaluation