title: Accessing a 2D array in a Razor View
body: <p>I'm attempting to create a booking form where the user can pick an hour slot.
  My idea is to create a ViewBag which will be a 2d array, which will contain a Booking object or a null reference. In the View I can then build a table using Razor. Each  will contain the booking information if present or a DateTime in the event a null reference to the array is detected. Clicking on DateTime cells will use javascript to provide a DateTime value to the form which will supply the booking information.</p>
  
  <p>My current problem is that I'm unable to access the array from within the view.</p>
  
  <p>Any suggestions on a better approach would be much appreciated.</p>
  
  <p>Here is what I have so far:
  The View:</p>
  
  <pre><code>@model Booking
  &lt;link rel="stylesheet" href="/Styles/Calendar.css"/&gt;
  &lt;div&gt;
      &lt;form action="Book" method="POST"&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="User"&gt;&lt;/label&gt;
              &lt;input asp-for="User" class="form-control"/&gt;
              &lt;button type="submit"&gt;Submit&lt;/button&gt;
          &lt;/div&gt;
          &lt;input asp-for="Slot" id="Slot" hidden="hidden"&gt;&lt;/p&gt;
      &lt;/form&gt;
  
      &lt;table class="table table-bordered"&gt;
          &lt;tr&gt;
              &lt;th&gt;&lt;/th&gt;
              &lt;th&gt;Monday&lt;/th&gt;
              &lt;th&gt;Tuesday&lt;/th&gt;
              &lt;th&gt;Wednesday&lt;/th&gt;
              &lt;th&gt;Thursday&lt;/th&gt;
              &lt;th&gt;Friday&lt;/th&gt;
              &lt;th&gt;Saturday&lt;/th&gt;
              &lt;th&gt;Sunday&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th&gt;&lt;/th&gt;
              @for(int day = 0; day &lt; 7; day++){
                  &lt;th&gt;@ViewBag.WkStart.AddDays(day).ToString("d")&lt;/th&gt;                
              }
          &lt;/tr&gt;
          @for(int x = 0; x &lt; 12; x++){           
              &lt;tr&gt;
                  &lt;td&gt;@ViewBag.WkStart.AddHours(x).ToString("h tt")&lt;/td&gt;
                  @for(int d = 0; d &lt; 7; d++){
                      @if(ViewBag.Dairy[x,d] != null){
                          &lt;td&gt;ViewBag.Diary[x,d].User&lt;/td&gt;
                      }
                      else{
                          &lt;td  onclick="SelectDate(this)"&gt;ViewBag.WkStart.AddDays(d).AddHours(x)&lt;/td&gt;
                      }
                  }               
              &lt;/tr&gt;
          }
      &lt;/table&gt;
  
  
  &lt;/div&gt;
  
  &lt;script src="/Scripts/Calendar.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>The Controller:</p>
  
  <pre><code>public class BookingController : Controller{
      private IRepository repository;
      private Booking[,] weekBooking = new Booking[12,7];
  
      public BookingController(IRepository repo){
          repository = repo;
      }
  
      public ViewResult Calendar(){
          DateTime date = DateTime.Now.Date.AddHours(6);
          DateTime wkStart;
          switch(date.DayOfWeek){
              case DayOfWeek.Monday:
                  wkStart = date;
                  break;
              case DayOfWeek.Tuesday:
                  wkStart = date.AddDays(-1);
                  break;
              case DayOfWeek.Wednesday:
                  wkStart = date.AddDays(-2);
                  break;
              case DayOfWeek.Thursday:
                  wkStart = date.AddDays(-3);
                  break;
              case DayOfWeek.Friday:
                  wkStart = date.AddDays(-4);
                  break;
              case DayOfWeek.Saturday:
                  wkStart = date.AddDays(-5);
                  break;
              case DayOfWeek.Sunday:
                  wkStart = date.AddDays(-6);
                  break;
              default:
                  wkStart = date;
                  break;
          }
          ViewBag.WkStart = wkStart;
  
          /* test booking */
          Booking trial = new Booking {
              BookingID = 111,
              User = "Dan",
              Slot = DateTime.Now.Date.AddHours(12)
          };
          weekBooking[1,1] = trial;
          ViewBag.Diary = weekBooking;
  
          return View();
      }
  }
  </code></pre>
  
  <p>Edit: Error message added.</p>
  
  <p>The Error message:</p>
  
  <pre><code>An unhandled exception occurred while processing the request.
  RuntimeBinderException: Cannot perform runtime binding on a null reference
  
  CallSite.Target(Closure , CallSite , object , int , int )
  
      Stack Query Cookies Headers 
  
      RuntimeBinderException: Cannot perform runtime binding on a null reference
          CallSite.Target(Closure , CallSite , object , int , int )
          System.Dynamic.UpdateDelegates.UpdateAndExecute3&lt;T0, T1, T2, TRet&gt;(CallSite site, T0 arg0, T1 arg1, T2 arg2)
          AspNetCore._Views_Booking_Calendar_cshtml+&lt;ExecuteAsync&gt;d__15.MoveNext() in Calendar.cshtml
  
                          @if(ViewBag.Dairy[x,d] != null){
  
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  </code></pre>
  
  <p>Screenshot for a better view:
  <a href="https://i.stack.imgur.com/mCZU1.png" rel="nofollow noreferrer">ErrorScreenshot</a></p>