SignalR MVC4 问题
SignalR MVC4 Issue
我正在按照本教程将 SignalR 集成到我的项目中 http://venkatbaggu.com/signalr-database-update-notifications-asp-net-mvc-usiing-sql-dependency/
所以基本上这是我的视图,我想在其中显示我的 table。
@{
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>
}
我的项目是 运行 但 table 根本没有出现。我从粘贴视图开始,因为我相信脚本一开始就没有执行;即使我尝试在
之后直接添加一个,也不会显示警报消息
$(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。
请帮助..
确保您已将视图中的 所有 脚本标记放置在视图的 scripts
部分内:
@section scripts {
... your <script> tags come here
}
您的警报不起作用的原因是您直接将它们放在视图的主体内,该视图在布局的 @RenderBody()
调用时呈现。但是正如您所看到的,只有在布局的末尾,我们才会引用 jQuery 和 signalr.
等脚本
现在它们将出现在正确的位置:@RenderSection("scripts", required: false)
。
顺便在您的网络浏览器中使用控制台 window 查看您可能遇到的潜在脚本错误。例如,在您的情况下,它会显示 jQuery is not defined 错误。
另一条评论:不要两次包含 signalR 脚本:现在您似乎已在视图中包含 jquery.signalR-2.2.0.js
并在布局中包含 jquery.signalR-2.2.0.min.js
。
我正在按照本教程将 SignalR 集成到我的项目中 http://venkatbaggu.com/signalr-database-update-notifications-asp-net-mvc-usiing-sql-dependency/
所以基本上这是我的视图,我想在其中显示我的 table。
@{
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>
}
我的项目是 运行 但 table 根本没有出现。我从粘贴视图开始,因为我相信脚本一开始就没有执行;即使我尝试在
之后直接添加一个,也不会显示警报消息$(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。 请帮助..
确保您已将视图中的 所有 脚本标记放置在视图的 scripts
部分内:
@section scripts {
... your <script> tags come here
}
您的警报不起作用的原因是您直接将它们放在视图的主体内,该视图在布局的 @RenderBody()
调用时呈现。但是正如您所看到的,只有在布局的末尾,我们才会引用 jQuery 和 signalr.
现在它们将出现在正确的位置:@RenderSection("scripts", required: false)
。
顺便在您的网络浏览器中使用控制台 window 查看您可能遇到的潜在脚本错误。例如,在您的情况下,它会显示 jQuery is not defined 错误。
另一条评论:不要两次包含 signalR 脚本:现在您似乎已在视图中包含 jquery.signalR-2.2.0.js
并在布局中包含 jquery.signalR-2.2.0.min.js
。