【问题标题】:SignalR MVC4 IssueSignalR MVC4 问题
【发布时间】: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>&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。 请帮忙..

【问题讨论】:

  • 您好像没有包含 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


【解决方案1】:

确保您已将视图中的 all 脚本标签放置在视图的 scripts 部分内:

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

您的警报不起作用的原因是因为您已直接将它们放在视图的主体中,该视图在布局的@RenderBody() 调用中呈现。但正如你所见,只有在这个 Layout 的末尾,我们才引用了 jQuery 和 signalr 等脚本。

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

顺便说一句,使用您的网络浏览器中的控制台窗口来查看您可能遇到的潜在脚本错误。例如,在您的情况下,它会显示 jQuery is not defined 错误。

另外一点:不要两次包含 signalR 脚本:现在您似乎已将 jquery.signalR-2.2.0.js 包含在您的视图中,并将 jquery.signalR-2.2.0.min.js 包含在您的布局中。

【讨论】:

  • 警告框现在显示,但表格还没有。所以我相信现在我可以继续调试代码,让脚本正常工作。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
相关资源
最近更新 更多