title: Checking to see if ViewBag has a property or not, to conditionally inject JavaScript
body: <p>Consider this simple controller:</p>
  
  <pre><code>Porduct product = new Product(){
    // Creating a product object;
  };
  try
  {
     productManager.SaveProduct(product);
     return RedirectToAction("List");
  }
  catch (Exception ex)
  {
     ViewBag.ErrorMessage = ex.Message;
     return View("Create", product);
  }
  </code></pre>
  
  <p>Now, in my <code>Create</code> view, I want to check <code>ViewBag</code> object, to see if it has <code>Error</code> property or not. If it has the error property, I need to inject some JavaScript into the page, to show the error message to my user.</p>
  
  <p>I created an extension method to check this:</p>
  
  <pre><code>public static bool Has (this object obj, string propertyName) 
  {
      Type type = obj.GetType();
      return type.GetProperty(propertyName) != null;
  }
  </code></pre>
  
  <p>Then, in the <code>Create</code> view, I wrote this line of code:</p>
  
  <pre><code>@if (ViewBag.Has("Error"))
  {
      // Injecting JavaScript here
  }
  </code></pre>
  
  <p>However, I get this error:</p>
  
  <blockquote>
    <p>Cannot perform runtime binding on a null reference</p>
  </blockquote>
  
  <p>Any idea?</p>