在格点击复选框,设置为true的Ajax MVC 5 [英] On div click set checkbox to true Ajax MVC 5

查看:115
本文介绍了在格点击复选框,设置为true的Ajax MVC 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示通知到我的网站的用户。当用户点击该按钮,通知它应该隐藏在柜台上。而仅显示通知的NotificationIsSeen是假的。

I need to display notifications to users of my site. When the user clicks the notify button it should hide the counter. And display only notifications that the NotificationIsSeen is false

我的模型

    //other fields
    [Display(Name = "Viewed")]
    public bool NotificationIsSeen { get; set; }

我的控制器修改

public ActionResult Index()
    {
       var notification = (from r in db.Notification
                            where r.NotificationIsSeen == false
                            select r);
        ViewBag.NotificationIsSeen = false;
        ViewBag.NotifyTotal = notification.Count();
        return View();
    }

我的看法修改

                            <!-- Menu toggle button -->
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <i class="fa fa-envelope-o" id="notify_button"></i>
                                 @if (ViewBag.NotifyTotal > 0)
                                {
                                    <span class="label label-success" id="notify_counter">@ViewBag.NotifyTotal</span>

                                }
                                else
                                {
                                    <span class="label label-success" id="notify_counter"></span>

                                }

                            </a>

我的AJAX code:

My ajax code:

<script>
    $('#notify_button').click(function () {
        $('#notify_counter').fadeOut('slow');
        $.ajax({
            url: "@(Url.Action("Index", "Home"))",
            type: 'POST',
            data: {
                NotificationIsSeen: "True" //I am not sure if this is correct

            }
        });
   });

的onclick的notify_button它隐藏了notify_counter,但它带来的再次刷新其网页上。

onclick the notify_button it hides the notify_counter, but it brings it again on refresh page.

你知道吗?
谢谢

Any idea? thank you

推荐答案

我终于找到解决办法也许是有用的人。

I finally find the solution maybe is useful for someone.

我的控制器

public ActionResult IsSeen()

    {
        var query = from r in db.Notification
                    where r.NotificationIsSeen == false
                    select r;

        foreach (Notification r in query)
        {
            r.NotificationIsSeen = true;


            }

        db.SaveChanges();
        return View();

    }

我的AJAX

<script>
    $('#notify_button').click(function () {
        $('#notify_counter').fadeOut('slow');

        $.ajax({
            url: "@(Url.Action("IsSeen", "Home"))",


          });
   });
    </script>

这篇关于在格点击复选框,设置为true的Ajax MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆