コード例 #1
0
        public async Task <IActionResult> Create([Bind("UserId,SurveyId,Answer")] User_Answers user_Answers)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user_Answers);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["UserId"]   = new SelectList(_context.Users, "Id", "Full_Name", user_Answers.UserId);
            ViewData["SurveyId"] = new SelectList(_context.Surveys, "Id", "Question", user_Answers.SurveyId);
            return(View(user_Answers));
        }
コード例 #2
0
 public void Delete(User_Answers user_answers)
 {
     _context.Remove(user_answers);
 }
コード例 #3
0
 public void Update(User_Answers user_answers)
 {
     _context.Update(user_answers);
 }
コード例 #4
0
 public void Add(User_Answers user_answers)
 {
     _context.Add(user_answers);
 }
コード例 #5
0
        public bool AnswerSubmit(PostVM VMobj)
        {
            User_Answers answers = new User_Answers();

            answers.QuestionId        = VMobj.questionId;
            answers.UserId            = VMobj.UserId;
            answers.SelectedAnswerIds = VMobj.answerValues;

            db.User_Answers.Add(answers);

            //increse NumOfSelectedAnswers by one
            string[] optionIds = VMobj.answerValues.Split(new Char[] { ';' });
            int[]    myIDs     = Array.ConvertAll(optionIds, s => int.Parse(s));
            var      q_answer  = db.Question_Answer_Values.Where(a => (myIDs).Contains(a.Id) && a.QuestionId == VMobj.questionId).ToList();

            q_answer.ForEach(a => a.NumOfSelectedAnswers++);
            //for (int i = 0; i < optionIds.Length; i++)
            //{
            //    var q_answer = db.Question_Answer_Values.Single(x => x.QuestionId == VMobj.questionId && x.Id ==int.Parse(optionIds[i]));
            //    if (q_answer.NumOfSelectedAnswers != null)
            //    {
            //        q_answer.NumOfSelectedAnswers += 1;
            //    }
            //    else
            //    {
            //        q_answer.NumOfSelectedAnswers = 1;
            //    }
            //}

            var question = db.Questions.Single(x => x.Id == VMobj.questionId);

            question.TotalAnswers += 1;
            var user = db.AspNetUsers.Find(VMobj.UserId);

            user.Points += 1;
            try
            {
                db.SaveChanges();
                return(true);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    string err = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                               eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        string errr = string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                    ve.PropertyName, ve.ErrorMessage);
                        throw new Exception(errr);
                    }
                }
                return(false);
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException e)
            {
                string err = e.Message;
                return(false);
            }
        }