0

I'm having difficulties figuring out how to solve this error message:

Error CS1579 foreach statement cannot operate on variables of type 'A1.Models.Reviews' because 'A1.Models.Reviews' does not contain a public definition for 'GetEnumerator' A1..NETCoreApp,Version=v1.0

@foreach (var item in Model)
                {
                    <content>
                        <header>
                            <hr id="ReviewsSide" />
                            <h6><b>&#64 @Html.DisplayFor(ModelItem => item.Heading)</b></h6>
                        </header>
                        <p>
                            Rating;

                            @{
                                int numStars = 0;

                                if (item.Rating == 5)
                                {
                                    numStars = 5;
                                }
                                else if (item.Rating == 4)
                                {
                                    numStars = 4;
                                }
                                else if (item.Rating == 3)
                                {
                                    numStars = 3;
                                }
                                else if (item.Rating == 2)
                                {
                                    numStars = 2;
                                }
                                else
                                {
                                    numStars = 1;
                                }



                                for (int i = 0; i < numStars; i++)
                                {
                                    <span style="color: #f8bd08; ">&#x2605;</span>
                                }
                                for (int i = 0; i < 5 - numStars; i++)
                                {
                                    <span>&#x2606;</span>
                                }


                            }
                        </p>
                 }

Any ideas?

I've added the following code to the top of my cshtml page but still the same error message:

IEnumerable<A1.Models.Reviews>
@model Reviews

Thankyou

ffxdean
  • 63
  • 1
  • 4
  • 13

2 Answers2

0

You need to replace

IEnumerable<A1.Models.Reviews>
@model Reviews

With

@model IEnumerable<A1.Models.Reviews>

In order to pass a sequence of reviews to your Razor View And be able to iterate over it.

The error shown means that the type being passed is not a sequence and though you can't iterate over it.

With that being said, you need also to make sure that you're passing a sequence from your controller to your View, so that these 3 parts work without any problem.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • That would just throw an exception unless the controller is also changed. –  Nov 12 '16 at 10:16
  • @StephenMuecke - I think this is the right answer as that model declaration by OP is definitely not correct. It seems OP is passing a list but the View cannot enumerate a single instance of that object as its currently declared, its needs an enumerable something of that object type. Honestly, I'm surprised the syntax he is using for model declaration didn't throw a runtime error. Sorry, was just trying to understand your comment better – Tommy Nov 12 '16 at 14:27
  • @Tommy OP is not currently passing a collection from the controller - just a single `Reviews`. If they were it were it would have throw [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](http://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) so OP must also change their GET method to return `IEnumerable` –  Nov 12 '16 at 20:53
0

Is Reviews a class you have written or a property (I suppose it is a class).

If my guess is right, I think your Reviews class should expose a property of type IEnumerable<Review> (or any class deriving like List<Review>). Your foreach loop in the view should loop on this property and not on the class instance.

If Reviews is a property, than you should confirm it is of an IEnumerable type.

Hope this will help

FrenchData
  • 630
  • 5
  • 10