public ActionResult EditPost(NoteEditViewModel model) { if (ModelState.IsValid) { var noteService = new NoteService(); var userId = Guid.Parse(User.Identity.GetUserId()); var result = noteService.Update(model, userId); TempData.Add("Result", result ? "Note updated." : "Note not updated."); return RedirectToAction("Index"); } return View(); }
/// <summary> /// Create a note. /// </summary> /// <param name="model"></param> /// <param name="userId"></param> /// <returns></returns> public bool Create(NoteEditViewModel model, Guid userId) { using (var context = new ElevenNoteDataContext()) { var note = new Note(); note.Title = model.Title; note.Contents = model.Contents; note.DateCreated = DateTime.UtcNow; note.ApplicationUserId = userId; note.IsFavorite = model.IsFavorite; context.Notes.Add(note); var result = context.SaveChanges(); return result == 1; } }
public bool Update(NoteEditViewModel model, Guid userId) { using (var context = new ElevenNoteDataContext()) { // Attempt to get the note from the database. var note = context.Notes.Where(w => w.Id == model.Id && w.ApplicationUserId == userId).SingleOrDefault(); // Functionally equivalent expressive syntax: //var note2 = (from w in context.Notes // where w.Id == model.Id && w.ApplicationUserId == userId // select w).SingleOrDefault(); // Make sure we actually received a note back before updating. if (note == null) return false; // Update the note. note.Contents = model.Contents; note.Title = model.Title; note.DateModified = DateTime.UtcNow; note.IsFavorite = model.IsFavorite; // Save the changes to the database. var result = context.SaveChanges(); return result == 1 /* was 1 record (success) or 0 records (unsuccessful) updated? */; } }