-1

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?

user2551750
  • 17
  • 1
  • 7

2 Answers2

0

x is null ? Causing your @Html.Display's to error.

I'd suggest a simple trial-and-error elimination process, i.e. comment out the DisplayFor's and see if the error disappears. This will help narrow down the cause.

Greg Trevellick
  • 1,361
  • 1
  • 16
  • 26
0

You are using "ViewData" instead of "ViewData["KeyName"]". If you are using only ViewData it will consider all ViewData values along with required data. for example WEBPAGE TITLE will also go into foreach loop. That may be causing the issue.

I would suggest you to please specify "ViewData["KeyName"]" instead of ViewData. So that the loop will run only for specified ViewData key.

Example Code which is working all fine for me is as below:

ViewData declaration and assignment

ViewDataDictionary vd = new ViewDataDictionary();
List<string> lst = new List<string>();
lst.Add("Data1User1");
lst.Add("Data2User1");
lst.Add("Data3User1");
lst.Add("Data4User1");
vd.Add("User1", lst);

lst = new List<string>();
lst.Add("Data1User2");
lst.Add("Data2User2");
vd.Add("User2", lst);

ViewData["vdt"] = vd;

Razor Code to loop through a list inside and another loop

@foreach (var vdt in ViewData["vdt"] as ViewDataDictionary)
{
    <h1 style="padding-bottom: 50px;"> @vdt.Key </h1>

    <table class="table">
        <tr style="border-bottom: 1px solid #333;">
            <th>Design
            </th>
        </tr>
        @foreach (var x in vdt.Value as List<string>)
        {
            <tr>
                <td>
                    @x
                </td>
            </tr>
        }
    </table>
}

Please follow above mentioned approach and correct you code.

Hope this will help you and please do let me know if you need any more information.

Deepak
  • 81
  • 5