title: Raises Object reference not set to an instance of an object - ASP.Net Core
body: <p>I have created a web page and it displays a data entering form. Its first element is a <code>textbox</code> and it will ask to enter ID of mother. After entering the Id it will post to the server to search is mother exist or not. If exist it returns data to the view and displays them on the form. I coded it according to my logic as follows. But when I run the page it raises an error "Object reference not set to an instance of an object" from the <code>View.cshtml</code> page.</p>
  
  <p>Controller.cs</p>
  
  <pre><code>public IActionResult Index(string id)
  {
      if (!string.IsNullOrEmpty(id))
      {
          SearchData(id); //This is asked to search mother
      }
      return View();
  }
  
  public void SearchData(string id)
  {
      using (_context)
      {
          if (!string.IsNullOrEmpty(id))
          {                    
              var Mother = _context.Mothers.Where(p =&gt; p.NIC == id).ToList();                   
              if (Mother.Any())
              {                        
                  ViewData["Mother"] = Mother;
              }
          }
      }
  }
  </code></pre>
  
  <p>Index.cshtml</p>
  
  <pre><code> @{ var m = ViewData["Mother"] as School_Mgt.Models.Students.Mother; }
  
   &lt;input asp-for="NICM" name="NICM" placeholder="NIC No." type="text" value="@m.NIC"&gt;  
   &lt;input asp-for="FNameM" name="FNameM" placeholder="Name in full" type="text" value="@m.FName"&gt;
    ...                                                                                     
  
  </code></pre>