2014-09-12 4 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를. NET 부분 파일은 _Layout.cshtml 파일에서 템플릿의 _loginPartial.cshtml이 있던 곳에 맨 위에 표시됩니다.

시작 단추를 누를 때 허브 코드가 맞지 않는 것이 문제입니다. 동일한 코드는 Index.html에 배치하면 작동합니다.

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

나는 부분 파일 실 거예요 허브를 시작하는 이유는 어떤 생각을 : 나는 JQuery와는 아래와 같이 레이아웃 파일의 맨 위에 추가 한? 사용자가보기에 상관없이 맨 위에 표시 할 메시지를 갖고 싶습니다. 그래서 레이아웃 파일에 배치 할 부분 파일을 사용하고 싶습니다.

답변

0

제대로 작동합니다.

<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> 
: 나는 아래로 내 _LoginPartial을 변경

<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> 

: 아래와 같은

_Layout.html 헤더 보인다