在主页

时间:2015-12-03 22:13:38

标签: jquery asp.net asp.net-mvc sql-server-2008 signalr

我正在使用信号r来显示通知。我的代码摘要是

  1. 我有一个从数据库中获取数据的hub方法。
  2. 在我的控制器发布方法中,我将数据保存到数据库
  3. 在我的通知页面视图中,我在按钮点击事件上有一个jquery代码来发送通知
  4. 在我的主页布局上,我有一个div选项卡,它接收通知(调用我的hub方法显示值)。目前它是div
  5. 我遇到的问题是我的jquery点击事件被调用并且调用了发送通知。但是没有调用将数据保存到表的http post事件。

    我想显示新的通知计数(比如红色),就像我们在facebook中一样,点击那个我想展开它并显示相同的内容。意味着我想实现相同的facebook / stacoverflow通知功能。这是一个在facebook中用于通知的div吗?

    任何人都可以给出一些关于我如何用Hubclass实现相同想法的想法:

    public class NotificationHub:Hub
    {
        private MyContext db = new MyContext();
    
        public void SendNotifications()
        {
            var ret = (from notification in db.Notifications.ToList()
                       select new
                       {
                           Message = notification.Text,
                           //PostedBy = post.PostedBy,
                           //PostedDate = post.PostedDate,
                         //  NotificationId = notification.NotificationID,
                       }).ToArray();
            //ret
            Clients.All.receiveNotification(ret);
        }
    

    Controller Post方法:

       [HttpPost]
        public ActionResult Notification(Notification notification)
        {
            if (ModelState.IsValid)
            {
                db.Notifications.Add(notification);
                db.SaveChanges();
    
                //NotificationHub obj = new NotificationHub();
                //obj.SendNotifications();
    
            }
    
            return View(notification);
        }
    

    查看调用jquery事件的位置

    @using (Html.BeginForm()){
       @Html.ValidationSummary(true)
      <div class="publishContainer">
       @Html.TextAreaFor(model => model.Text, new { @placeholder = "Enter notification here!!" })
    </div>
      <p>
       <input id="button1" type="button" value="button" />
    
    @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;// Start the connection.$.connection.hub.start().done(function () { $("#button1").click(function () { notifications.server.sendNotifications(); }).fail(function (e) {  alert(e);
    });
     });
    });
    </script>
    

    和我的主页

     $(function () {
        var proxy = $.connection.notificationHub;
    
        proxy.client.receiveNotification = function (data) {
            for (var i = 0; i < data.length; i++) {
                var element = data[i];
                $('#discussion').append('<li>' + element.Message + '</li>');
            }
    

    此处通知未保存到数据库。 我想显示新的通知计数(比如红色),就像我们在facebook中一样,点击那个我想展开它并显示相同的内容。意味着我想实现相同的facebook / stacoverflow通知功能。这是一个在facebook中用于通知的div吗?

0 个答案:

没有答案