1
我有一個使用SignalR的ASP.NET MVC應用程序。當處理SignalR的客戶端腳本是內聯腳本時,它工作正常,但如果我將腳本解壓縮到.js文件,它不會接收來自服務器的調用。在.js文件中的腳本中,SignalR在MVC中沒有響應
@model LiveContentSample.Models.Entry
@{
ViewBag.Title = "Details SignalR";
ViewBag.EntryId = Model.Id;
}
<h2>Details with PartialView SignalR Comments</h2>
<fieldset>
<legend>Entry</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.Title)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Title)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Content)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Content)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>
@Html.Action("_GetForEntryWithSignalR", "Comment", new { entryId = @Model.Id })
@section scripts {
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-ui-1.8.24.js"></script>
<script src="~/Scripts/jquery.signalR-1.1.3.min.js"></script>
<script src="~/signalr/hubs"></script>
@* <script type="text/javascript" src="~/Scripts/CommentsSignalR.js"></script>*@
<script>
$(function() {
var entryId = '@ViewBag.EntryId';
var commentHub = $.connection.commentHub;
commentHub.client.send = function (content) {
$("#comments-list").append("<li>" + content + "</li>");
};
$.connection.hub.start().done(function() {
commentHub.server.register(entryId);
$("#add-comment").click(function() {
commentHub.server.addComment(entryId, $("#content").val());
});
});
});
</script>
}
但是,如果我將最終腳本移出到.js文件,它將不起作用。有任何想法嗎?
謝謝@Jacob,我知道這很簡單。有沒有通過entryId的標準方法? –
我通常在Razor中放入一個小內聯腳本,它只是在外部腳本中調用一個函數,傳入由Razor生成的參數。 – Jacob