title: ASP.NET MVC: JavaScript error when starting index.cshtml instead of _Layout.cshtml
body: <p>When I start Home/index.cshtml (in Visual Studio 2013 by pressing F5) the website crashes with this error:</p>
  
  <blockquote>
    <p>0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference</p>
  </blockquote>
  
  <p>But when I start the website Shared/_Layout.cshtml it works fine.</p>
  
  <p><strong>Home/index.cshtml:</strong></p>
  
  <pre><code>@{
      ViewBag.Title = "Home Page";
  }
  
  &lt;div class="jumbotron"&gt;    
      &lt;p id="greet-message" class="lead"&gt;&lt;/p&gt;   
  &lt;/div&gt;
  
  &lt;!-- Scripts --&gt;
  &lt;script type="text/javascript"&gt;
          $(function () {
              // declare a reference to loginhub
              console.log("Logging in ...");
  
              // ***!!!*** HERE IT CRASHES! ***!!!***
              var login = $.connection.loginHub;
  
              //get's called when the user have been authenticated i.e. logged in
              login.client.greet = function (message) {
                  $("#greet-message").html(message);
              };
  
              //start the hub
              $.connection.hub.start().done(function () {
                  login.server.greet();
              });
  
          });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Shared/_Layout.cshtml:</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;title&gt;@ViewBag.Title - My ASP.NET Application&lt;/title&gt;
      @Styles.Render("~/Content/css")
      @Scripts.Render("~/bundles/modernizr")
      @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/bootstrap")
      &lt;script src="Scripts/jquery.signalR-2.2.0.min.js"&gt;&lt;/script&gt;
      &lt;script src="signalr/hubs"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="navbar navbar-inverse navbar-fixed-top"&gt;
          &lt;div class="container"&gt;
              &lt;div class="navbar-header"&gt;
                  &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  @Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
              &lt;/div&gt;
              &lt;div class="navbar-collapse collapse"&gt;
                  @Html.Partial("_LoginPartial")
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="container body-content"&gt;
          @RenderBody()
      &lt;/div&gt;
  
      @RenderSection("scripts", required: false)
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>_ViewStart.cshtml</strong></p>
  
  <pre><code>@{
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  </code></pre>
  
  <p>By default Shared/_Layout.cshtml loads Home/index.cshtml.</p>
  
  <p>I thought it might be because the document wasn't ready so I changed the first function in index.cshtml to use <code>$( document ).ready(function()</code> but with no difference.</p>
  
  <p>I used jQuery 1.10 but I downgraded it to 1.8.2 after this post but with no difference: <a href="https://stackoverflow.com/questions/15749471/jquery-validation-error-in-asp-net-mvc-unable-to-get-value-of-property-call">jquery validation error in Asp.net MVC - unable to get value of property &#39;call&#39; : object is null or undefined</a></p>
  
  <p>I am using <code>jquery.signalR-2.2.0</code> if that might cause some problems.</p>
  
  <p>What is the difference from starting the website from index.cshtml instead of _Layout.cshtml?</p>