2014-09-12 84 views
0

不工作我在我的ASP.NET部分文件下面的代码:SignalR代码在ASP.NET MVC中的部分文件

<ul class="nav navbar-nav navbar-right"> 
<li><label id="message">No messages</label></li> 
<li><input type="button" id="startButton" value="Start Monitoring" /></li> <!--Just for testing actual app won't have a button! --> 

@section scripts { 
<!--Script references. --> 
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. --> 
<!--Reference the SignalR library. --> 
<script src="~/Scripts/jquery.signalR-2.0.2.js"></script> 
<!--Reference the autogenerated SignalR hub script. --> 
<script src="~/signalr/hubs"></script> 
<!--SignalR script to update the chat page and send messages.--> 
<script> 

    $(function() { 
     // Reference the auto-generated proxy for the hub. 
     var testHub = $.connection.testHub; 

     // Create a function that the hub can call back to display messages. 
     testHub.client.displayMessages = function (message) { 
      // Add the message to the page. 
      $('#message').text(message); 
     }; 

     // Start the connection. 
     $.connection.hub.start().done(function() { 
      $('#startButton').click(function() { 
       // Call the Send method on the hub. 
       testHub.server.listenToMessages(1); 
      }); 
     }); 
    }); 

</script> 

}

的ASP。在_Layout.cshtml文件中引用.NET部分文件以在顶部显示_loginPartial.cshtml用于模板中的位置。

问题是当我按下startbutton时,Hub代码没有命中。当我放入Index.html时,它的代码是相同的。我已经JQuery的增加在顶部的布局文件象下面这样:

@Styles.Render("~/Content/css") 
@Scripts.Render("~/bundles/modernizr") 
@Scripts.Render("~/bundles/jquery") 

任何想法,为什么我的部分文件无法启动的枢纽?我的想法是,无论他在哪个视图中,我都希望将消息显示在顶部。这就是为什么我想要将部分文件放置在布局文件中的原因。

回答

0

明白了。

_Layout.html头看起来如下:

<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>@ViewBag.Title - My ASP.NET Application</title> 
    @Styles.Render("~/Content/css") 
    @Scripts.Render("~/bundles/modernizr") 
    @Scripts.Render("~/bundles/jquery") 
    <script src="~/Scripts/jquery.signalR-2.0.2.min.js"></script> 
    <script src="~/signalr/hubs"></script> 
</head> 

我改变了我的_LoginPartial如下:

<ul class="nav navbar-nav navbar-right"> 
    <li> 
     @Html.ActionLink("Hello !", "Index", "Manage", routeValues: null, htmlAttributes: new { @id = "message", title = "Manage" }) 
    </li> 
</ul> 
<script> 

    $(function() { 
     // Reference the auto-generated proxy for the hub. 
     var testHub = $.connection.testHub; 

     // Create a function that the hub can call back to display messages. 
     testHub.client.displayMessage = function (message) { 
      // Add the message to the page. 
      $('#message').text(message); 
     }; 

     // Start the connection. 
     $.connection.hub.start().done(function() { 
      testHub.server.hello(); 
     }); 
    }); 

</script>