Beispiel #1
0
        public ActionResult Create(int surveyId, string action, Response model)
        {
            // Before saving the current response - need to delete any existing responses for ANYONE who is
            // registered at the current user's address.
            string currentUserId = User.Identity.GetUserId();
            // Get Address for the current user.
            ApplicationUser currentUser = _db.Users.FirstOrDefault(u => u.Id == currentUserId);
            var             addressId   = currentUser.AddressId;
            // Get all users at this address.
            var userIdsAtAddress = _db.Users.Where(u => u.AddressId == addressId).Select(u => u.Id).ToList();

            // Find all responses for this survey by any of the users at this address.
            var responsesToDelete = _db.Responses.Where(r => r.SurveyId == surveyId && userIdsAtAddress.Contains(r.CreatedByUserId)).ToList();

            if (responsesToDelete.Count > 0)
            {
                _db.Responses.RemoveRange(responsesToDelete);
                _db.SaveChanges();
            }

            // Add the latest response.
            model.Answers         = model.Answers.Where(a => !String.IsNullOrEmpty(a.Value)).ToList();
            model.SurveyId        = surveyId;
            model.CreatedByUserId = User.Identity.GetUserId();
            model.CreatedBy       = UserManager.FindById(model.CreatedByUserId).Name;
            model.CreatedOn       = DateTime.Now;
            _db.Responses.Add(model);
            _db.SaveChanges();

            TempData["success"] = "Your response was successfully saved!";

            return(RedirectToAction("Index", "Surveys"));
        }
        public ActionResult Create(Survey survey, string action)
        {
            if (ModelState.IsValid)
            {
                survey.CreatedByUserId = User.Identity.GetUserId();

                survey.Questions.ForEach(q => q.CreatedOn = q.ModifiedOn = DateTime.Now);
                _db.Surveys.Add(survey);
                _db.SaveChanges();
                TempData["info"] = "The survey was successfully created!";
                return(RedirectToAction("Edit", new { id = survey.Id }));
            }
            else
            {
                TempData["error"] = "An error occurred while attempting to create this survey.";
                return(View(survey));
            }
        }