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

25,155

Solution 1

You've passed the result which is a type of ResultsClass not a collection of ResultsClass, and then attempt to iterate over it as if it were a collection so you can't use it in your foreach loop. So in order to be able to use foreach on it, you have to make it a collection. For example your foreach loop works if you would passed results instead of result:

return View(results);

@model List<Project.Models.ResultsClass>

@foreach (var item in Model) {
}

Solution 2

This line is the culprit:

@model Project.Models.ResultsClass

That line then leads to problems in your controller action if you are wanting to return a list (collection) to the view.

Basically in the view, that line means that you are wanting to show 1 ResultsClass object.. not a collection of ResultsClass objects, so you can't iterate over just 1 ResultsClass object.

But for now, you could resolve that issue by just calling the properties of the ResultsClass object you're returning to the view.

@model Project.Models.ResultsClass

@Html.DisplayFor(modelItem => item.previewID)
@Html.DisplayFor(modelItem => item.dateSlot)
@Html.DisplayFor(modelItem => item.timeSlot)
@Html.DisplayFor(modelItem => item.startTime)
@Html.DisplayFor(modelItem => item.dateCreated)

OR

Just pass the results object in your controller action:

public ActionResult Results()
{
    var results = db.Data.SqlQuery("SELECT * FROM Results").ToList();
    return View(results);
}

Then in your view:

@model IEnumerable<Project.Models.ResultsClass>

@foreach (var item in Model) {
    // .. display properties
}

Let me know if this helps.

Share:
25,155
user979331
Author by

user979331

Updated on July 17, 2022

Comments

  • user979331
    user979331 almost 2 years

    I am getting an error here in my razor view:

    foreach statement cannot operate on variables of type 'Project.Model.ResultsClass' because 'Project.Model.ResultsClass' does not contain a public definition for 'GetEnumerator'

    Here is my foreach and how I am defining Model

    @model Project.Models.ResultsClass
    
    @foreach (var item in Model) {
    }
    

    Here is the controller method:

    public ActionResult Results()
    {
        var results = db.Data.SqlQuery("SELECT * FROM Results").ToList();
        ResultsClass result = new ResultsClass();
    
        result.previewID = results[0].id;
        result.dateSlot = results[0].dateSlot;
        result.timeSlot = results[0].timeSlot;
        result.startTime = results[0].startTime;
        result.dateCreated = results[0].dateCreated;
    
        return View(result);
    }
    

    And Here is my class:

    public class ResultsClass
    {
        [DisplayName("Preview ID")]
        public int previewID { get; set; }
        [DisplayName("Date")]
        public string dateSlot { get; set; }
        [DisplayName("Time")]
        public string timeSlot { get; set; }
        [DisplayName("Start Time")]
        public DateTime startTime { get; set; }
        [DisplayName("Completed Time")]
        public DateTime dateCreated { get; set; }
    }
    

    I don't understand what I am doing wrong?

    • Dushan Perera
      Dushan Perera over 6 years
      Your view is strongly typed to a single instance of ResultsClass. Not a collection. Why are you looping if you are not passing a collection ?
    • user979331
      user979331 over 6 years
      Ahhhh I see. So I need to pass the variable result to my view so I can loop through it?
    • Dushan Perera
      Dushan Perera over 6 years
      yes. return View(results) and change the view to have a collection of ResultsClass
    • David
      David over 6 years
      Why do you expect to be able to loop over a single object? What behavior do you expect this to produce?
  • user979331
    user979331 over 6 years
    Ahhhh I see. So I need to pass the variable result to my view so I can loop through it?
  • user979331
    user979331 over 6 years
    Ahhhh I see. So I need to pass the variable result to my view so I can loop through it?
  • tonythewest
    tonythewest over 6 years
    It sounds like that is what you wish to do. Otherwise you could leave it as is and change your loop to @foreach (var item in Model.result){ }
  • Grizzly
    Grizzly over 6 years
    Can you show some code to better define your answer.