public ActionResult SetImagePreference(string imageName, string userName, bool likeImage) { if (!string.IsNullOrEmpty(imageName) && !string.IsNullOrEmpty(userName)) { var imageList = userImageService.GetUserImages(); if (imageList.Any(x => x.ImageName == imageName && x.UserName == userName)) { var exsitingItem = userImageService.GetUserImageByImageNameAndUsername(imageName, userName); if (exsitingItem.LikeIt != likeImage) { exsitingItem.LikeIt = likeImage; userImageService.SaveChange(); } } else { var userImage = new UserImage() { ImageName = imageName, UserName = userName, LikeIt = likeImage }; userImageService.CreateNewUserImage(userImage); } var returnModel = new UserImage() { ImageName = imageName, UserName = userName, LikeIt = likeImage }; return(new JsonResult() { Data = returnModel, JsonRequestBehavior = JsonRequestBehavior.AllowGet }); //using (var context = new UserImageContext()) //{ // if (context.UserImages.Any(x => x.ImageName == imageName && x.UserName == userName)) // { // var exsitingItem = context.UserImages.FirstOrDefault(x => x.ImageName == imageName && x.UserName == userName); // if (exsitingItem.LikeIt != likeImage) // { // exsitingItem.LikeIt = likeImage; // context.SaveChanges(); // } // } // else // { // var userImage = new UserImage() // { // ImageName = imageName, // UserName = userName, // LikeIt = likeImage // }; // context.UserImages.Add(userImage); // context.SaveChanges(); // } // var returnModel = new UserImage() // { // ImageName = imageName, // UserName = userName, // LikeIt = likeImage // }; // return new JsonResult() { Data = returnModel, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; //} } return(null); }