// GET: Reports/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Report report = db.Reports.Find(id); if (report == null) { return(HttpNotFound()); } //本人の日報でなければ表示しないように、それをEdit.cshtmlにTempDataで伝える。 if (report.EmployeeId != User.Identity.GetUserId()) { TempData["wrong_person"] = "true"; } ReportsEditViewModel editViewModel = new ReportsEditViewModel { Id = report.Id, ReportDate = report.ReportDate, Title = report.Title, Content = report.Content, NegotiationStatus = report.NegotiationStatus, AttendanceTime = report.AttendanceTime, LeavingTime = report.LeavingTime }; return(View(editViewModel)); }
public ActionResult Edit([Bind(Include = "Id,ReportDate,Title,Content")] ReportsEditViewModel editViewModel) { if (ModelState.IsValid) { Report report = db.Reports.Find(editViewModel.Id); report.ReportDate = editViewModel.ReportDate; report.Title = editViewModel.Title; report.Content = editViewModel.Content; report.UpdatedAt = DateTime.Now; db.Entry(report).State = EntityState.Modified; db.SaveChanges(); // TempDataにフラッシュメッセージを入れておく。 TempData["flush"] = "日報を編集しました。"; return(RedirectToAction("Index")); } return(View(editViewModel)); }