Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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>
                        }
kevin hudson
kevin hudson
Courses Plus Student 11,584 Points

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