Reputation: 169
In my view I have passed a view Model why I am getting the Error
result of a query cannot be enumerated more than once
Can anyone please help me on this.
Here is my View
@model App.ViewModels.ViewTeamList
<div class="row">
<div class="col-md-12 col-sm-12">
@if (Model.TeamList.Count() > 0)
{
<div class="table-responsive">
@{
var grid = new WebGrid(source: Model.TeamList.ToList(), defaultSort: "TeamName", canPage: true, rowsPerPage: 10);
}
@grid.WebGridSelectAll(
headerStyle: "gridHeader",
tableStyle: "table table-condensed table-striped table-bordered table-hover no-margin",
checkBoxValue: "TeamId",
columns: new[]{
grid.Column("TeamName",format: @<a href="#" class="details" data-id="@item.TeamId">@item.TeamName</a>,header: Html.CustomText("lblCTTeamName", "Team Name")),
grid.Column("Description",format: @<a href="#" class="details" data-id="@item.TeamId">@item.Description</a>, header: Html.CustomText("lblDescription", "Description"), canSort: false),
grid.Column("UserCount",format: @<a href="#" class="details" data-id="@item.TeamId">@item.UserCount</a>, header: Html.CustomText("lblCTUserCount", "# of User(s)"))
}
)
</div>
}
else
{
<div>No Record Found</div>
}
</div>
</div>
ViewModel
public class ViewTeamList
{
public IEnumerable<TeamList> TeamList;
public Team Team { get; set; }
[Required]
public string SearchTeamName { get; set; }
}
Upvotes: 2
Views: 262
Reputation: 239430
TeamList
is an IEnumerable
. An enumerable differs from something like a list in that its data structure is set up to pop items from the stack. Once an item has been popped it's essentially disposed, meaning you can only run through the items once.
Here, you're doing this by calling .Count()
. In order to get a count from an enumerable, the items have to be iterated over, which effectively uses up your enumeration. Then, when you try to run it through WebGrid
, you get that error. You either need to cast TeamList
to a list and store the result:
@{ var teamList = Model.TeamList.ToList(); }
@if (teamList.Count() > 0)
{
...
Or, probably better, just change the type of the property on your view model:
public List<TeamList> TeamList { get; set; }
Upvotes: 4