2014-03-28 54 views
1

我有默认的Visual Studio 2013 MVC模板。我在使用ASP.NET Identity登录并导航到我创建的Match.cshtml页面后出现错误“JavaScript运行时错误:'$'未定义”。任何想法为什么它没有看到jquery'$'?MVC 5 ASP.NET身份和信号R

[编辑] 如果我制作一个普通的HTML页面,一切工作,所以这种方式必须关联到使用布局或周围的东西。我正在使用默认模板_Layout页面。

@using Microsoft.AspNet.Identity 


@if (Request.IsAuthenticated) 
{ 
<!--Script references. --> 
<!--Reference the jQuery library. --> 
<script src="Scripts/jquery-2.1.0.min.js"></script> 

<!--Reference the SignalR library. --> 
<script src="Scripts/jquery.signalR-2.0.3.min.js"></script> 

<!--Reference the autogenerated SignalR hub script. --> 
<script src="signalr/hubs"></script> 

<!--Add script to update the page and send messages.--> 
<script type="text/javascript"> 
    $(function() { 

     var clientID = ""; 
     var server = $.connection.gameServer; 

     // Create a function that the hub can call to broadcast messages. 
     server.client.broadcastMessage = function (name, message) { 

      // Html encode display name and message. 
      var encodedName = $('<div />').text(name).html(); 
      var encodedMsg = $('<div />').text(message).html(); 

      // Add the message to the page. 
      $('#discussion').append('<li><strong>' + encodedName 
       + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); 
     }; 

     // get the ID from the server for our team 
     server.client.responseID = function (id) { 
      clientID = id; 
     } 

     // Start the connection. 
     $.connection.hub.start().done(function() { 

      server.server.requestID(); 

      // todo: define all the GUI events that need to send requests to the server for validation 
      $('#sendmessage').click(function() { 
       // Call the Send method on the hub. 
       server.server.send($('#displayname').val(), $('#message').val()); 

       // Clear text box and reset focus for next comment. 
       $('#message').val('').focus(); 
      }); 
     }); 
    }); 
</script> 
} 
else 
{ 
    <div>Please login!</div> 
} 

回答

1

我需要将我的脚本包装在脚本部分的Match视图中。

@section scripts{ 
@Scripts.Render("~/Scripts/jquery.signalR-2.0.3.min.js") 
@Scripts.Render("~/signalr/hubs") 
etc...