0

我想在 ApplyLeave 表更改时发送通知。场景就像......当用户申请休假时,经理会收到“有人申请休假”的通知。我正在努力解决如何做到这一点。我是首先使用代码和 SignalR 发送通知。我不擅长 javascript。提前致谢。

我的 ApplyLeave 课程

 public class ApplyLeave

{
      public long? EmployeeId { get; set; }
      public long? EmployeeLeaveTypesId { get; set; }
      public bool Approved { get; set; }
      public bool ViewedByManager { get; set; }
      public string ManagerRemark { get; set; }
}

我的通知类

public class NotificationHub:Hub
    {

        public void Send(string name, string message)
        {
            // Call the addNewMessageToPage method to update clients.
            Clients.All.addNewMessageToPage(name, message);
        }
    }

在视图中,我有一个 id="notify" 的按钮。单击按钮时,我正在使用 ajax 请求保存数据。在 ajax 成功事件中,我需要向经理发送通知....我真的不知道如何去做这个

<script type="text/javascript">
       $(document).ready(function ()
    {
 $('#notify').click(function () {

        var params = {
            "ViewByManager": $('#viewbymanager').val(),
            "ManagerRemark": $('#managerremark').val()
        };

        var dataToPost = JSON.stringify(params);
        $.ajax({
            type: "POST",
            url: "/ApplyLeave/Apply",
            contentType: "application/json; charset=utf-8",
            data: dataToPost,
            dataType: "json",
            success: function (data, textStatus){

 var  notification = $.connection.notificationHub; 
                    // send notification to the manager

                },

            });

        });
       });
        </script>
4

1 回答 1

1

你看到聊天教程了吗,你可以在这里应用相同的逻辑。 这是教程

我想你能做的是,

//send the user name to the server to broadcast to the connected client.
notification.send(user_name_whois_applying, some_msg);

在经理看来,

//connect to the same hub and call the client method.

$(function () {
    var notification = $.connection.notificationHub; ;
    notification.addNewMessageToPage= onAddMessage;

    $.connection.hub.start();
});
function onAddMessage(message) {
    // Add the message to somewhere
     $('#messages').append( message);
}

我认为这至少应该让你开始:)

于 2013-10-05T18:38:06.050 回答