如何在控制器事后调用按钮点击事件?

时间:2015-12-08 12:14:41

标签: jquery asp.net-mvc-3 model-view-controller signalr signalr-hub

如何在控制器发送后调用按钮点击事件?

这是我的视图,我有一个文本框来输入通知messafe,按钮点击它应该将数据保存到数据库,并应使用脚本部分中的代码将通知发送给客户端。这里它调用http post事件,但jquery中的代码也会触发但是通知没有发送。  notifications.server.sendNotifications();没有解雇。

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <div class="publishContainer">
            @Html.TextAreaFor(model => model.Text, new { @placeholder = "Whats in your mind !!" })</div>
        <p>
            <input id="button1" type="submit" value="button" />
        </p>
    }
    @section Scripts {
        <script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
        <script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script>
        <script src="~/signalr/hubs"></script>
        <script type="text/javascript">
            $(function () {
                var notifications = $.connection.notificationHub;
                debugger;
                // Start the connection.
                $.connection.hub.start().done(function () {
                    $("#button1").click(function () {
                        notifications.server.sendNotifications();

                    }).fail(function (e) {
                        alert(e);
                    });
                });
            });
        </script>
    }

    and the controller Post event 

     [HttpPost]
            public ActionResult Notification(Notification notification)
            {
                if (ModelState.IsValid)
                {
                    db.Notifications.Add(notification);
                    db.SaveChanges();

                    //var hub = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
                    //hub.Clients.All.sendNotifications();

                    //NotificationHub obj = new NotificationHub();
                    //obj.SendNotifications();

                }

                return View(notification);
            }

This will call the http Post event but it is not firing the jquery event where i have the signal r hub methos.

0 个答案:

没有答案