public ActionResult CreateEdit(Receipt receipt) { if (ModelState.IsValid) { if (receipt.ID == 0) { //Create db.Receipts.Add(receipt); } else { //Edit //Tried this based on feedback from Alper //db.MarkAsModified(receipt); //Custom copy from intro to code first with multiple objects db.CopyChanges(receipt.ID, receipt); } //Validation errors should be caught and can be interrigated with //((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors db.SaveChanges(); return RedirectToAction("Index"); } PopulateTransactionTypeDropDown(receipt.TransactionType); return View(receipt); }
// GET: Receipts/CreateEdit/5 //Need to start both here so they return here. Kind of makes sense anyway //https://evolpin.wordpress.com/2011/05/09/mvc-and-posting-data-using-html-beginform-and-url-routing/ public ActionResult CreateEdit(int? id) { Receipt receipt; if (id != null) receipt = db.Receipts.Where(c => c.ID == id).Include("Categories").SingleOrDefault(); else receipt = new Receipt(); PopulateTransactionTypeDropDown(receipt.TransactionType); return View("CreateEdit", receipt); }
public IHttpActionResult PostReceipt(Receipt receipt) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Receipts.Add(receipt); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = receipt.ID }, receipt); }
public void CopyChanges(int id, Receipt receipt) { Receipt recToModify = this.Receipts.Where(c => c.ID == id).Include("Categories").SingleOrDefault(); List<Category> categoriesForReceipt = this.Receipts.Where(c => c.ID == id).Include("Categories").SingleOrDefault().Categories; List<Category> toDelete = new List<Category>(); recToModify.AccountNumber = receipt.AccountNumber; recToModify.StoreName = receipt.StoreName; recToModify.Amount = receipt.Amount; recToModify.Approver = receipt.Approver; recToModify.TransactionDate = receipt.TransactionDate; recToModify.TransactionType = receipt.TransactionType; //If in DB but not new data, delete foreach (var oldC in categoriesForReceipt) { if (receipt.Categories == null || receipt.Categories.Find(c => c.ID == oldC.ID) == null) toDelete.Add(oldC); } //Now remove them, cant remove from original while iterating foreach (var d in toDelete) categoriesForReceipt.Remove(d); //add or update if (receipt.Categories != null) { foreach (var newC in receipt.Categories) { var cToUpdate = categoriesForReceipt.Find(c => c.ID == newC.ID); if (cToUpdate != null) { cToUpdate.Amount = newC.Amount; cToUpdate.Description = newC.Description; } else { //we get an ID of 0 from the UI on multiple adds newC.ID = -1; categoriesForReceipt.Add(newC); } } } }
public void MarkAsModified(Receipt item) { Entry(item).State = EntityState.Modified; }
public void MarkAsModified(Receipt item) { }
public Receipt GetSingleReceipt() { Receipt r = new Receipt("55", DateTime.Parse("11/2/2015"), 1, "Best Buy", 5.56m, new List<Category> { new Category("Room", 5.56m) }, "Tom"); r.ID = 3; return r; }
public void CopyChanges(int i, Receipt r) { }
public IHttpActionResult PutReceipt(int id, Receipt receipt) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != receipt.ID) { return BadRequest(); } //idea from //http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application //tried this but added everytime //db.Categories.AddOrUpdate(c => c.ID, receipt.Categories.ToArray()); db.CopyChanges(id, receipt); //was //db.Entry(receipt).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ReceiptExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }