【发布时间】:2015-04-26 15:28:37
【问题描述】:
我正在按照本教程将 SignalR 集成到我的项目 http://venkatbaggu.com/signalr-database-update-notifications-asp-net-mvc-usiing-sql-dependency/
所以基本上这是我想要展示我的表格的视图。
@{
ViewBag.Title = "PatientInfo";
}
<h2>PatientInfo</h2>
<h3>@ViewBag.pName</h3>
<h5>@ViewBag.glucoseT</h5>
@if (Session["LogedUserFirstname"] != null)
{
<text>
<p>Welcome @Session["LogedUserFirstname"].ToString()</p>
</text>
@Html.ActionLink("Log Out", "Logout", "Home")
<div class="row">
<div class="col-md-12">
<div id="messagesTable"></div>
</div>
</div>
<script src="/Scripts/jquery.signalR-2.2.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/SignalR/Hubs"></script>
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var notifications = $.connection.dataHub;
//debugger;
// Create a function that the hub can call to broadcast messages.
notifications.client.updateMessages = function () {
getAllMessages()
};
// Start the connection.
$.connection.hub.start().done(function () {
alert("connection started")
getAllMessages();
}).fail(function (e) {
alert(e);
});
});
function getAllMessages() {
var tbl = $('#messagesTable');
$.ajax({
url: '/home/GetMessages',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
}).error(function () {
});
}
</script>
}
我的项目正在运行,但表格根本没有出现。我从粘贴视图开始,因为我相信脚本一开始就没有执行;即使我尝试在
之后直接添加警告消息,也不会显示警告消息$(function () {
alert("I am an alert box!");
这是我的 Layout.cshtml 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css")
<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="~/Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<header>
<div class="content-wrapper">
</div>
</header>
<div id="body">
@RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© @DateTime.Now.Year - My ASP.NET MVC Application</p>
</div>
</div>
</footer>
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="SignalR/Hubs"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#p_table").dataTable();
});
</script>
@RenderSection("scripts", required: false)
</body>
</html>
我正在使用 Visual Studio 2012,MVC4。 请帮忙..
【问题讨论】:
-
您好像没有包含 jQuery?除非它在您的 _Layout.cshtml 文件中
-
我添加了这个 jquery.signalR-2.2.0.js 并在我的布局视图中添加了 jquery-1.9.1.min.js
-
我认为您可能在 jQuery 之前引用了 SignalR - jQuery 是 SignalR 的依赖项。这也解释了为什么在
doc.ready上没有执行任何操作。在引用 jQuery 之后,将对 SignalR 脚本和 Hub 代理的引用移动到布局视图中 -
我做了还是没用..我还加了 $(document).ready(.. 但是什么都没有:(
-
看看我的代码github.com/adaam2/APoorMansTwitterStreamingClient/blob/master/… - 我的布局视图和外部 JavaScript 文件中都引用了所有内容!
标签: javascript asp.net-mvc-4 signalr signalr-hub sqldependency