title: how to handle null exception if model is null , how to handle it in view in mvc?
body: <p>In an MVC application in the controller I use Entity Framework and LINQ. I'm storing 10 records in a variable then, binding them to the model. But if there are no records I'm getting error </p>
  
  <blockquote>
    <p>Index was out of range. Must be non-negative and less than the size of
    the collection.</p>
  </blockquote>
  
  <p>While I handled null exception in view. I'm getting compile time error </p>
  
  <blockquote>
    <p>Cannot initialize implicitly typed variable with an array initializer.</p>
  </blockquote>
  
  <p><strong>View:</strong> </p>
  
  <pre><code>  function initMap() {
              var labels = '12345678910';
              var labelIndex = 0;
  
             @if (Model != null)
                     {
                         var   myLatLng = { lat:  @Model[0].latitude.ToString(), lng: @Model[0].longitude.ToString() };
                     }
  
             else
                     {
                            var myLatLng = { lat:0, lng:0};//Bharat Seva Ashram   
                     }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>List&lt;AssetTrackerViewModel&gt; model = new List&lt;AssetTrackerViewModel&gt;();
  /// PIR 1 //RAD:DN
  try
  {
      WebRequest req = WebRequest.Create(@"https://url");
      req.Method = "GET";
      req.Headers["Authorization"] = "Basic " + "pwd==";
      HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
      var encoding = resp.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(resp.CharacterSet);
      using (var stream = resp.GetResponseStream())
      {
          var reader = new StreamReader(stream, encoding);
          var responseString = reader.ReadToEnd();
  
        **//here "items" im getting null / empty** 
         var items = Pirs.Where(a =&gt; !a.dataFrame.EndsWith("AAAAAAAAAAA="))
                                           .GroupBy(a =&gt; a.dataFrame.Substring(a.dataFrame.Length - 12))
                                           .Select(g =&gt; g.First())
                                           .OrderByDescending(a =&gt; a.timestamp)
                                           .Take(10);
  
          foreach (var item in items)
          {
              byte[] data = Convert.FromBase64String(item.dataFrame.ToString());
          }
      }
  }
  </code></pre>