在 .js 文件中编写脚本时,信号R 在 MVC 中没有响应
SignalR not responding in MVC when script in a .js file
我有一个使用SignalR的 ASP.NET MVC应用程序。 当处理 SignalR 的客户端脚本是内联的时,它工作正常,但如果我将脚本提取到.js文件,它不会收到来自服务器的调用。
@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文件中,它不起作用。 有什么想法吗?
@ViewBag.EntryId
是一个Razor指令。如果将脚本移动到JS文件中,Razor将不再运行任何内容。如果需要将此脚本移动到单独的文件,则必须以某种方式将该条目 ID 传递到脚本中。
相关文章:
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 通过mvc中的ajax动作链接获取关于成功的响应头
- 获取从 .post() 到 PHP 文件的响应,但只能在 MVC 中获取
- 如何将Spring MVC的JSON响应放入jsp中的表中
- 预检响应具有无效的 HTTP 状态代码 405 - AngularJS + Spring MVC
- jQuery ajax 响应绑定到 MVC 模型表中
- 在 .js 文件中编写脚本时,信号R 在 MVC 中没有响应
- 在网站 mvc 中接收 ajax 响应
- 如何在 C# MVC 中使用 BBC 的 Imager.Js 作为响应式图像
- ASP.NET MVC ActionResult如何返回数据以响应GET请求
- 在jQuery对话框中重新加载Spring MVC响应页面
- 如何在jquery文件ulpload成功后从c#MVC控制器获得响应
- MVC Jquery在部分更新后没有响应
- 如何向asp.net MVC 4控制器发送ajax请求以请求XML响应
- MVC 4部分视图导致页面在提交时变得没有响应
- Ajax.处理两个不同的onSuccess响应,MVC 5, c#
- 表单上的MVC提交打开JQuery对话框,等待用户响应
- 保持所有的JavaScript在我的页面底部使用响应.. net MVC中的过滤器
- 在spring MVC中以AJAX响应发送文件
- 加载资源失败:服务器响应状态为500(内部服务器错误)MVC JAVASCRIPT