public ActionResult Edit(CauHoi cauHoi, DapAn dapan1, DapAn dapan2, DapAn dapan3, DapAn dapan4, HttpPostedFileBase file) { try { if (db.CauHois.Any(x => x.MaCauHoiMoi == cauHoi.MaCauHoiMoi && x.MaCauHoi != cauHoi.MaCauHoi)) { ModelState.AddModelError("MaCauHoiMoi", "Mã câu hỏi này đã tồn tại. Bạn hãy kiểm tra lại."); } List <DapAn> dapAns = new List <DapAn>(); if (dapan1.MaDapAn != 0) { dapAns.Add(dapan1); } if (dapan2.MaDapAn != 0) { dapAns.Add(dapan2); } if (dapan3.MaDapAn != 0) { dapAns.Add(dapan3); } if (dapan4.MaDapAn != 0) { dapAns.Add(dapan4); } if (dapAns.Any(x => x.DapAnDung == true) == false) { ModelState.AddModelError("", "Bạn cần chỉ định ít nhất 1 đáp án là đáp án đúng!"); } if (ModelState.IsValid) { foreach (DapAn dapAnItem in dapAns) { db.Entry(dapAnItem).State = System.Data.Entity.EntityState.Modified; } if (file != null && file.ContentLength > 0) { string fileName = DateTime.Now.Ticks.ToString() + System.IO.Path.GetExtension(file.FileName); cauHoi.Hinh = fileName; file.SaveAs(Server.MapPath("~/Content/images/" + fileName)); } db.Entry(cauHoi).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Edit", new { Id = cauHoi.MaCauHoi, message = "Sửa thành công" })); } ViewBag.MaChuyenMuc = new SelectList(db.ChuyenMucCauHois, "MaChuyenMuc", "TenChuyenMuc", cauHoi.MaChuyenMuc); cauHoi.DapAns = db.CauHois.Find(cauHoi.MaCauHoi).DapAns.ToList(); return(View(cauHoi)); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); ViewBag.MaChuyenMuc = new SelectList(db.ChuyenMucCauHois, "MaChuyenMuc", "TenChuyenMuc", cauHoi.MaChuyenMuc); cauHoi.DapAns = db.CauHois.Find(cauHoi.MaCauHoi).DapAns.ToList(); return(View(cauHoi)); } }
public IHttpActionResult PutAnswer(int id, Answer answer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != answer.Id) { return(BadRequest()); } db.Entry(answer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!AnswerExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void UpdateOutput(Participant model) { using (QuizDBEntities db = new QuizDBEntities()) { db.Entry(model).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
public ActionResult Edit(ThongTinChung thongTinChung) { if (ModelState.IsValid) { db.Entry(thongTinChung).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(Redirect("Edit")); } return(View(thongTinChung)); }
public DataModel.Users Update(DataModel.Users obj) { tblUser user = new tblUser(); user.UserId = obj.UserId; user.UserName = obj.UserName; user.FirstName = obj.FirstName; user.LastName = obj.LastName; user.UserEmail = obj.Email; user.CreatedBy = obj.CreatedBy; user.CreatedAt = obj.CreatedAt; user.UpdatedBy = obj.UpdatedBy; user.UpdatedAt = obj.UpdatedAt; user.UserRole = obj.Role; user.Password = obj.Password; user.IsActive = obj.IsActive; context.Entry(user).State = EntityState.Modified; context.SaveChanges(); return(obj); }
public DataModel.Quiz Update(DataModel.Quiz obj) { tblQuiz quiz = context.tblQuizs.Where(q => q.ID == obj.ID).FirstOrDefault(); quiz.Name = obj.Name; quiz.Description = obj.Description; quiz.AllowReAttempt = obj.AllowReAttempt; quiz.Hours = obj.Hours; quiz.Minutes = obj.Minutes; quiz.ReAttemptDuration = obj.ReAttemptDuration; quiz.Timer = obj.Time; quiz.PassMarks = obj.PassMarks; quiz.QuizUrl = obj.QuizUrl; quiz.CreatedBy = obj.CreatedBy; quiz.CreatedAt = DateTime.Now; quiz.Type = obj.Type; quiz.UpdatedAt = DateTime.Now; quiz.IsActive = obj.IsActive; quiz.QuizNotes = obj.QuizNotes; quiz.RequiresLogin = obj.RequiresLogin; context.Entry(quiz).State = EntityState.Modified; int result = context.SaveChanges(); if (result > 0) { obj.Success = true; obj.ErrorMessage = "Quiz updated successfully"; } else { obj.Success = false; obj.ErrorMessage = "An error occured while updating quiz."; } return(obj); }
public IHttpActionResult PutQuestion(int id, Question question) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != question.Id) { return(BadRequest()); } db.Entry(question).State = EntityState.Modified; foreach (var answer in question.Answers) { db.Entry(answer).State = EntityState.Modified; } try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!QuestionExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult ChangePassword(ChangePasswordViewModel changePasswordViewModel) { QuanTriVien quanTriVien = Session["UserLogged"] as QuanTriVien; if (changePasswordViewModel.OldPassword != quanTriVien.MatKhau) { ModelState.AddModelError("OldPassword", "Mật khẩu hiện tại không đúng!"); } if (ModelState.IsValid) { quanTriVien.MatKhau = changePasswordViewModel.NewPassword; db.Entry(quanTriVien).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); Session["UserLogged"] = quanTriVien; return(RedirectToAction("Index", "Dashboard")); } return(View(changePasswordViewModel)); }
public ActionResult Edit([Bind(Include = "Id,Email,PlainPassword,AvatarId,AvatarImage,UserTypeId,UserType,NickName,NumberOfFinishedQuiz,TotalScore")] UserViewModel userViewModel) { if (ModelState.IsValid) { User user = new User(); user.Email = userViewModel.Email; user.NickName = userViewModel.NickName; user.Password = userViewModel.PlainPassword; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AvatarId = new SelectList(db.Avatar, "Id", "Name", userViewModel.AvatarId); ViewBag.UserTypeId = new SelectList(db.UserType, "Id", "Name", userViewModel.UserTypeId); return(View(userViewModel)); }
public ActionResult Edit([Bind(Include = "Id,Name,DataInHttpPostedFileBase")] AvatarViewModel avatarViewModel) { if (ModelState.IsValid) { Avatar avatar = new Avatar(); if (avatarViewModel.DataInHttpPostedFileBase != null) { avatar.Data = ConvertHttpPostedFileBaseToByteArray(avatarViewModel.DataInHttpPostedFileBase); } avatar.Name = avatarViewModel.Name; db.Entry(avatar).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(avatarViewModel)); }
public DataModel.Questions UpdateQuestion(DataModel.Questions obj) { tblQuizQuestion Question = context.tblQuizQuestions.Where(q => q.QuestionId == obj.QuestionId).FirstOrDefault(); if (Question == null) { return(obj); } Question.QuestionId = obj.QuestionId; Question.QuizId = obj.QuizId; Question.QuestionText = obj.QuestionText; Question.IsActive = obj.IsActive; Question.UpdatedAt = DateTime.Now; Question.UpdatedBy = obj.UpdatedBy; context.Entry(Question).State = EntityState.Modified; //context.Entry(question).CurrentValues.SetValues(Question); int saved = context.SaveChanges(); if (saved > 0) { obj.UpdatedAt = Question.UpdatedAt.Value; obj.CreatedAt = Question.CreatedAt.Value; obj.Success = true; obj.ErrorMessage = "Question updated successfully."; } else { obj.UpdatedAt = Question.UpdatedAt.Value; obj.CreatedAt = Question.CreatedAt.Value; obj.Success = false; obj.ErrorMessage = "Error occured while updating question."; } return(obj); }
public void Update(TEntity item) { _context.Entry(item).State = EntityState.Modified; _context.SaveChanges(); }
public ActionResult NewQuizStart(QuizTestViewModel quiz) { // Create a list what is used for counting the total score per User per Quiz List <Question> questionList = db.Question.Include(q => q.Answer).AsEnumerable().Where(x => x.QuizId == quiz.Question[0].QuizId).ToList(); var quizId = quiz.Question.FirstOrDefault().QuizId; var quizName = db.Quiz.Where(x => x.Id == quizId).Select(x => x.Name).FirstOrDefault(); var userId = quiz.Question.FirstOrDefault().UserId; var userNickName = db.User.Where(x => x.Id == userId).Select(x => x.NickName).FirstOrDefault(); // Create unique model instance CompletedQuiz completedQuiz = new CompletedQuiz(); // Create database list to save User's answers var dbList = db.FinishedQuizByUser.ToList(); int totalScore = 0; for (int i = 0; i < quiz.Question.Count; i++) { completedQuiz = new CompletedQuiz { UserId = userId, UserNickName = userNickName, QuizId = quizId, QuizName = quizName, QuestionId = quiz.Question[i].QuestionId, Date = quiz.Question[i].Date, }; if (quiz.Question[i].QuestionType == 1) { if (quiz.Question[i].SelectedAnswer == questionList[i].Answer.Where(x => x.AnswerCheck == true).FirstOrDefault().Id) { totalScore += questionList[i].Point; completedQuiz.Score = questionList[i].Point; } } if (quiz.Question[i].QuestionType == 2) { int trueAnswerCounterInDatabase = 0; int correctAnswerCounterFromUser = 0; foreach (var item in questionList[i].Answer) { if (item.AnswerCheck == true) { trueAnswerCounterInDatabase++; } } for (int j = 0; j < quiz.Question[i].AnswerList.Count; j++) { if (quiz.Question[i].AnswerList[j].AnswerCheck == questionList[i].Answer.ElementAt(j).AnswerCheck&& questionList[i].Answer.ElementAt(j).AnswerCheck == true) { correctAnswerCounterFromUser++; if (correctAnswerCounterFromUser == trueAnswerCounterInDatabase) { completedQuiz.Score = questionList[i].Point; totalScore += questionList[i].Point; break; } } } } FinishedQuizByUser result = new FinishedQuizByUser(); result.UserId = completedQuiz.UserId; result.QuizId = completedQuiz.QuizId; result.QuestionId = completedQuiz.QuestionId; result.Score = completedQuiz.Score; result.Date = completedQuiz.Date; var finishedQuiz = db.FinishedQuizByUser?.Where(q => q.QuestionId == completedQuiz.QuestionId && q.UserId == completedQuiz.UserId)?.FirstOrDefault(); if (finishedQuiz == null) { db.FinishedQuizByUser.Add(result); db.SaveChanges(); } else { finishedQuiz.QuestionId = completedQuiz.QuestionId; finishedQuiz.Score = completedQuiz.Score; finishedQuiz.Date = completedQuiz.Date; db.Entry(finishedQuiz).State = EntityState.Modified; db.SaveChanges(); } } completedQuiz.TotalScore = totalScore; return(View("FinishedQuizView", completedQuiz)); }