Example #1
0
        public ActionResult Index(Models.like Li)   //通过AJAX在后台访问
        {
            if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request.IsAuthenticated)
            {
                // 检查当前用户是否已为一个已登录用户
                //bool isAuthenticated = System.Web.HttpContext.Current.Request.IsAuthenticated;

                // 获取当前请求的用户名
                string userName = System.Web.HttpContext.Current.User.Identity.Name;

                Li.UserName = userName;
                db.likes.Add(Li);

                db.items.Find(Li.ItemId).totalLike += 1;
                db.SaveChanges();
            }


            return(View());                      //?
        }
Example #2
0
        public ActionResult Index(Models.like Li)   //通过AJAX在后台访问
        {
            int x = 0;

            if (System.Web.HttpContext.Current == null)
            {
                return(Json(x));
            }
            if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request.IsAuthenticated)
            {
                // 检查当前用户是否已为一个已登录用户
                //bool isAuthenticated = System.Web.HttpContext.Current.Request.IsAuthenticated;



                // 获取当前请求的用户名
                string userName = System.Web.HttpContext.Current.User.Identity.Name;

                Li.UserName = userName;
                x           = -1;
                var query = from like in db.likes
                            where (like.UserName == userName) && (like.ItemId == Li.ItemId)
                            select like;

                if (query.Count() == 0)
                {
                    x = 1;
                    db.likes.Add(Li);

                    db.items.Find(Li.ItemId).totalLike++;
                    db.SaveChanges();
                }

                return(Json(x));
            }

            return(Json(x));
            //?
        }