mardi 8 décembre 2015

How to call button click event after controller postevent?

How to call button click event after controller postevent?

here is my view where i have a text box to enter notification messafe and on button click it should save data to database and should send the notification to clients using the code in the script section . Here it calls the http post event but the code in the jquery also firing but the notification is not sending . notifications.server.sendNotifications(); is not firing .

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

Aucun commentaire:

Enregistrer un commentaire