gpt4 book ai didi

javascript - SignalR MVC4 问题

转载 作者:行者123 更新时间:2023-12-03 10:15:46 25 4
gpt4 key购买 nike

我正在按照本教程将 SignalR 集成到我的项目 http://venkatbaggu.com/signalr-database-update-notifications-asp-net-mvc-usiing-sql-dependency/

所以基本上这是我想要显示我的表格的 View 。

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



}

我的项目正在运行,但该表根本没有出现。我首先粘贴 View ,因为我相信脚本一开始就没有执行;即使我尝试在

之后直接添加警报消息,也不会显示警报消息
$(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>&copy; @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。请帮忙..

最佳答案

确保您已将 View 中的所有脚本标记放置在 View 的 scripts 部分内:

@section scripts {
... your <script> tags come here
}

您的警报不起作用的原因是您直接将它们放入 View 的主体内,该主体在布局的 @RenderBody() 调用中呈现。但正如您所看到的,只有在此布局的末尾,我们才引用了 jQuery 和 signalr 等脚本。

现在它们将出现在正确的位置:@RenderSection("scripts", required: false)

顺便说一下,使用网络浏览器中的控制台窗口来查看可能存在的潜在脚本错误。例如,在您的情况下,它将显示 jQuery 未定义错误。

另一个备注:不要两次包含 signalR 脚本:现在您似乎已在 View 和 jquery.signalR-2.2 中包含了 jquery.signalR-2.2.0.js 。布局中的 0.min.js

关于javascript - SignalR MVC4 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com