Пример #1
0
        public async Task<ActionResult> ChangeActivities(ChangeActivitiesViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            //Get the old user so we have the fields for it, then update the privacy setting and save to the DB
            var currentUser = UserManager.FindById(User.Identity.GetUserId());

            List<UserRecreation> userRecList = new List<UserRecreation>();

            foreach (var userRec in model.RecOptions)
            {
                if (userRec.IsChecked)
                {
                    // Check if this user is already associated with this rec type in the db. If not, save it now.
                    var userRecs = db.UserRecreations.Where(u => u.User == currentUser.Id && u.RecreationID == userRec.RecreationID);

                    if (userRecs.Count() < 1)
                    {
                        UserRecreation ur = userRec;
                        ur.User = currentUser.Id;
                        db.UserRecreations.Add(ur);
                    }
                }
                else 
                {
                    // Make sure this rec type is not associated with the user. If it is, remove it now.
                    var userRecs = db.UserRecreations.Where(u => u.User == currentUser.Id && u.RecreationID == userRec.RecreationID);

                    if (userRecs.Count() > 0)
                    {
                        UserRecreation ur = userRecs.First();
                        db.UserRecreations.Remove(ur);
                    }
                }

                db.SaveChanges();
            }
            
            return RedirectToAction("Index", new { Message = ManageMessageId.ChangeActivitiesSuccess });
            
        }
Пример #2
0
        //
        // GET: /Manage/ChangeActivities
        public ActionResult ChangeActivities()
        {
            var userId = User.Identity.GetUserId();

            List<UserRecreation> userRecList = new List<UserRecreation>();
            List<Recreation> recs = db.Recreations.ToList();
            var userRecs = db.UserRecreations.Where(u => u.User == userId);
            
            foreach (var rec in recs)
            {
                UserRecreation ur = new UserRecreation();
                ur.RecreationID = rec.RecreationID;
                ur.RecreationLabel = rec.Label;

                // If they already have this rec type, make the box checked already.
                //var r = db.UserRecreations.Where(u => u.User == userId && u.RecreationID == rec.RecreationID);
                if (userRecs.Where(u => u.RecreationID == rec.RecreationID).Count() > 0)
                {
                    ur.IsChecked = true;
                } 

                userRecList.Add(ur);
            }

            var model = new ChangeActivitiesViewModel
            {
                RecOptions = userRecList
            };
            return View(model);
        }