Search code examples
asp.net-mvcasp.net-mvc-3viewenumerable

MVC view and enumerate a list


Using MVC3 I have this in my controller. How do I enumerate this within the view?

    public ActionResult NWaySwitch()
    {
        var switches = _nWaySwitch.Switches.Cast<Switch>().ToList(); 
       // var switches = _nWaySwitch.Switches;
        return View(switches);
    }

foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

@foreach (System.Collections.ArrayList item in ViewData["switches"]) {}


Solution

  • I am guessing you made a strongly typed view here right? If you look at the top of the view you will probably see the Model definition. Make sure that you make this in a IEnumerable<> and you should be good to go