I have created a list inside another list like so:
var collectionsList = db.Data.ToList().Select(m => new { m.collection, m.hidden }).Where(model => model.hidden == false).Distinct();
foreach (var item in collectionsList)
{
ViewData.Add(new KeyValuePair<string, object>(item.collection, db.Data.ToList().Where(model => model.collection == item.collection)));
}
return View();
Now in my view I am trying to loop through ViewData and then loop through the lists inside the ViewData like so:
var collectionsList = db.Data.ToList().Select(m => new { m.collection, m.hidden }).Where(model => model.hidden == false).Distinct();
foreach (var item in collectionsList)
{
ViewData.Add(new KeyValuePair<string, object>(item.collection, db.Data.ToList().Where(model => model.collection == item.collection)));
}
return View();
@foreach (var item in ViewData)
{
<h1 style="padding-bottom:50px;">item.Key</h1>
<table class="table">
<tr style="border-bottom: 1px solid #333;">
<th>
Design
</th>
<th>
Size (sq ft.)
</th>
<th style="text-align: right;">
Price
</th>
</tr>
@foreach(var x in item.Value as List<CP.Models.Pricing>)
{
<tr>
<td>
@Html.DisplayFor(modelItem => x.name)
</td>
<td>
@Html.DisplayFor(modelItem => x.sqft) sq ft.
</td>
<td style="text-align: right;">
$@Html.DisplayFor(modelItem => x.basePrice)
</td>
</tr>
}
</table>
}
But I get this error on the second loop: Object reference not set to an instance of an object.
Why am I getting this error? and how can I fix it?