public ActionResult AuditForm(AuditDetailViewModel model) { if (!ModelState.IsValid) { return View(model); } var auditor = this.GetAuthUser(); var context = ApplicationDbContext.Create(); var audit = context.Audits.Single(x => x.Id == model.AuditId); audit.Auditor = context.Users.Single(x => x.Id == auditor.Id); foreach (var item in model.Items) { var auditItem = audit.Items.Single(x => x.Id == item.Id); auditItem.AuditorValue = item.AuditorValue; auditItem.Comment = item.Comment; } context.SaveChanges(); return RedirectToAction("CompanyList"); }
public ActionResult AuditForm(string userId) { var auditor = this.GetAuthUser(); var context = ApplicationDbContext.Create(); var questionnaire = context.Questionnaires.Single(x => x.User.Id == userId); var audit = context.Audits.SingleOrDefault(x => x.User.Id == questionnaire.User.Id); if (audit == null) { audit = new Audit() { User = context.Users.Single(x=>x.Id == userId), Auditor = context.Users.Single(x=>x.Id== auditor.Id), }; foreach (var item in questionnaire.Items) { audit.Items.Add(new AuditItem() { Key = item.Key, UserValue = item.Value, }); } context.Audits.Add(audit); context.SaveChanges(); } var model = new AuditDetailViewModel(audit); return View(model); }