Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# ASP.NET MVC Forms Improving Our Form Using Radio Buttons

A way to loop through enum rather than typing each value?

Rather than using:

<div>
    <label class="radio-inline">
        @Html.RadioButtonFor(m => m.Intensity, Entry.IntensityLevel.Low)                                     
        @Entry.IntensityLevel.Low
    </label>
    <label class="radio-inline">
        @Html.RadioButtonFor(m => m.Intensity, Entry.IntensityLevel.Medium)    
        @Entry.IntensityLevel.Medium
    </label>
    <label class="radio-inline">
        @Html.RadioButtonFor(m => m.Intensity, Entry.IntensityLevel.High) 
        @Entry.IntensityLevel.High
    </label>
</div>

Is there a way to loop through the IntensityLevel enum and access their values using an index?

2 Answers

Benjamin Lange
Benjamin Lange
16,178 Points

Absolutely! Take a look at this StackOverflow question.

Basically, something like this:

foreach(Foos foo in Enum.GetValues(typeof(Foos)))
Adrian Maleszewski
Adrian Maleszewski
1,402 Points
@foreach (Entry.IntensityLevel intensity in Enum.GetValues(typeof(Entry.IntensityLevel)))
                        {
                        <label class="radio-inline">
                            @Html.RadioButtonFor(m => m.Intensity, intensity.ToString())
                            @intensity.ToString()
                        </label>
                        }

Flags Entry level saying namespace could not be found for Entry level