Reputation: 26386
I am trying to pass some list to my view through ViewModel. I have rebuilt my project several times to no avail. I have checked online, nothing different from my approach.
namespace sportingbiz.Models.ViewModels
{
public class MatchesViewModel
{
private List<MatchViewModel> _matches = new List<MatchViewModel>();
public List<MatchViewModel> Matches
{
get { return _matches; }
set { _matches = value; }
}
}
}
Markup
@model IEnumerable<sportingbiz.Models.ViewModels.MatchesViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table style="width:100%">
@foreach (var item in Model.Matches)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Team1Name)
</td>
</tr>
}
If I remove the .Matches
from the foreach
it works.Matches
does not show in the intellisense.
Upvotes: 0
Views: 60
Reputation: 93434
IEnumerable does not contain a method called Matches. So naturally, trying to reference it won't work.
It's like taking a bag of chips and trying to get a chip by grabbing the bag, rather than reaching inside the bag.
I don't think you want to pass an IEnumerable of your viewmodel to the view. I think you just want to pass the view model itself.
Upvotes: 1
Reputation: 2361
Your model is IEnumerable<sportingbiz.Models.ViewModels.MatchesViewModel>
which is a series of MatchesViewModels. You need to enumerate that list first before you can call Matches
. If you're only returning one MatchesViewModel, change your model declaration to:
@model sportingbiz.Models.ViewModels.MatchesViewModel
Edit - A trick I use often is to hover over any of the instances of Model
and VS will tell you what type the model is.
Upvotes: 5