I’m passing this collection to my view:
List <UserGoalsStepViewModel> userGoals = new List<UserGoalsStepViewModel>(); //I populate this List with ViewModel objects and then pass it to the view... foreach (UseCase uc in useCases) { userGoals.Add (new UserGoalsStepViewModel { GoalTitle = uc.Title, ActorTitle = uc.actor.Title, }); } return View(userGoals);
Then, in my declaration in my View I have this:
@model IEnumerable<JustSpecIt.ViewModels.UserGoalsStepViewModel>
However, when I use a `@foreach` in my View, I get the dreaded red squiggle telling me I can’t use foreach because the variable type doesn’t contain a definition for GetEnumerator() – I thought the model declaration in the View would have taken care of that?
What should I change?
Help much appreciated!
gazrolo4
What should I change?
gazrolo4
return View(userGoals);
return View(userGoals.AsEnumerable());
Thanks for the response but I added that and it didn’t make a difference I’m afraid. Would I be better using some other type of collection in the first place, rather than a list?
gazrolo4
@model IEnumerable<JustSpecIt.ViewModels.UserGoalsStepViewModel>
@model List<JustSpecIt.ViewModels.UserGoalsStepViewModel>
That stops my view recognising any of my model properties
show your view and action code
Hi gazrolo4,
There is the GetEnumerator method in IEnumerable interface.
# IEnumerable.GetEnumerator Method
http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator(v=vs.110).aspx
Please check your code. The code could be like this:
@foreach(UserGoalsStepViewModel item in Model) { <div>item.Title</div> }
Best Regards
Starain