public ActionResult Edit(int?id, IncidentEdit newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.Id })); } if (id.GetValueOrDefault() != newItem.Id) { // This appears to be data tampering, so redirect the user away return(RedirectToAction("index")); } if (newItem.DocUpload.ContentType != "application/pdf") { return(RedirectToAction("edit", new { id = newItem.Id })); } for (int i = 0; i < newItem.StudentIds.Count(); i++) { newItem.StudentIds.Remove(""); newItem.StudentNames.Remove(""); } // Attempt to do the update var editedItem = m.IncidentEdit(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.Id })); } else { // Show the details view, which will have the updated data return(RedirectToAction("details", new { id = newItem.Id })); } }
// ############################################################ public IncidentWithDetails IncidentEdit(IncidentEdit newItem) { var o = ds.Incidents.Include("Students").Include("Instructor").SingleOrDefault(a => a.Id == newItem.Id); if (o == null) { return(null); } else { //update o.Students.Clear(); o.description = newItem.description; o.campus = newItem.campus; o.program = newItem.program; if (newItem.DocUpload != null) { byte[] docBytes = null; docBytes = new byte[newItem.DocUpload.ContentLength]; newItem.DocUpload.InputStream.Read(docBytes, 0, newItem.DocUpload.ContentLength); // Then, configure the new object's properties o.Doc = docBytes; o.DocContentType = newItem.DocUpload.ContentType; } if (newItem.status.ToLower() == "open" || newItem.status.ToLower() == "closed") { o.status = newItem.status; } foreach (var tuple in newItem.StudentIds.Zip(newItem.StudentNames, Tuple.Create)) { var a = ds.Students.SingleOrDefault(x => x.studentId == tuple.Item1); if (a != null) { if (a.name == tuple.Item2) { o.Students.Add(a); } } } /* * SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); * smtpClient.EnableSsl = true; * MailMessage msg = new MailMessage(); * string body; * * Instructor myInstructor; * myInstructor = ds.Instructors.SingleOrDefault(i => i.Id == newItem.Id); * * if(myInstructor != null) * { * msg.To.Add(myInstructor.emailAddress); * msg.Subject = "Seneca Academic Honesty Notice"; * body = "Hello Professor " + myInstructor.name; * body += "\n\n"; * body += "\tThe student has lost marks"; * body += "\n\n"; * body += "\tCase has been closed"; * * msg.Body = body; * * smtpClient.Send(msg); * } * * newItem.status = "closed"; */ ds.SaveChanges(); return(Mapper.Map <IncidentWithDetails>(o)); } }