title: how to show viewbag data in html table by converting viewbag object to list in asp.net mvc
body: <p>im passing list data in the viewbag from the controller . while accessing the viewbag as list getting error.</p>
  <pre><code>error - 'Object reference not set to an instance of an object.'
  </code></pre>
  <p>getting error at this line. <code>ViewBag.Total_PowerList as IEnumerable&lt;TransformerEnergyMonitoring.Models.KWHConsumption&gt;</code></p>
  <p><a href="https://i.stack.imgur.com/IXZwe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IXZwe.png" alt="screenshot" /></a></p>
  <p>view</p>
  <pre><code>@model IEnumerable&lt;TransformerEnergyMonitoring.Models.KWHConsumption&gt;
      &lt;div class=&quot;row&quot;&gt;
       
              &lt;div class=&quot;table-responsive&quot;&gt;
                  &lt;table id=&quot;myTable&quot; class=&quot;table table-bordered table-hover table-striped&quot;&gt;
                      &lt;thead class=&quot;table-header&quot;&gt;
                          &lt;tr&gt;
                              &lt;th&gt;Date&lt;/th&gt;
                              &lt;th&gt;KWH Reading&lt;/th&gt;
                              &lt;th&gt;KWH Consumption&lt;/th&gt;
                          &lt;/tr&gt;
                      &lt;/thead&gt;
                      &lt;tbody&gt;
                              @foreach (var item in (ViewBag.Total_PowerList as IEnumerable&lt;TransformerEnergyMonitoring.Models.KWHConsumption&gt;))
                              {
                                  &lt;tr&gt;
                                      &lt;td&gt;@item.timeStamp&lt;/td&gt;
                                  &lt;/tr&gt;
                              }
                      &lt;/tbody&gt;
                  &lt;/table&gt;
              &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  <blockquote>
  <p>dataset</p>
  </blockquote>
  <pre><code>{ timeStamp = 12/5/2020 12:00:00 AM, finalState = 47326.95, consuption = 1455.71 }
  </code></pre>
  <blockquote>
  <p>Controller</p>
  </blockquote>
  <pre><code>public ActionResult KWHDateWise(RangeSelection RS)
      {
          DateTime ToDate = Convert.ToDateTime(RS.ToDate).AddDays(1);
          RS.ToDate = ToDate;
  
          List&lt;KWHConsumption&gt; kwhcon = new List&lt;KWHConsumption&gt;();
          
        var Total_PowerList = db.Total_Power.Where(u =&gt; u.DeviceImei == RS.DeviceImei &amp;&amp; (u.DeviceTimeStamp &gt;= RS.FromDate &amp;&amp; u.DeviceTimeStamp &lt;= RS.ToDate)).OrderByDescending(u =&gt; u.DeviceTimeStamp).ToList();
  
          ViewBag.Total_PowerList= Total_PowerList
  .GroupBy(
      i =&gt; i.DeviceTimeStamp.Date,
      (timeStamp, dayilyData) =&gt; new { timeStamp, dayilyData })
  .Select(i =&gt; new
  {
      i.timeStamp,
      finalState = i.dayilyData.Max(x =&gt; x.KWH),
      consuption = (i.dayilyData.Max(x =&gt; x.KWH) - i.dayilyData.Min(x =&gt; x.KWH))
  })
  .ToList();
          return PartialView(&quot;~/Views/TransEnergyHistory/_KWH.cshtml&quot;, kwhcon);
      }
  </code></pre>
  <p>Model</p>
  <pre><code> public class KWHConsumption
      {
         
          public DateTime timeStamp { get; set; }
          public double finalState { get; set; }
          public double consuption { get; set; }
  
      }
  </code></pre>