public ActionResult Create([Bind(Include = "Id,CustomerId,PetId,DateofSale")] Sale sale) { if (ModelState.IsValid) { db.Sales.Add(sale); SoldPet sold = new SoldPet(); Pet pet = db.Pets.Find(sale.PetId); if (pet != null) { sold.Id = pet.Id; sold.CategoryId = pet.CategoryId; sold.Gender = pet.Gender; sold.Birthdate = pet.Birthdate; sold.Price = pet.Price; db.SoldPets.Add(sold); db.Pets.Remove(pet); } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CustomerId = new SelectList(db.Customers, "Id", "Name", sale.CustomerId); ViewBag.PetId = new SelectList(db.Pets, "Id", "Id", sale.PetId); return(View(sale)); }
// GET: Sales/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } SoldPet sold = db.SoldPets.Find(id); if (sold == null) { return(HttpNotFound()); } return(View(sold)); }
// GET: Sales/Details/5 public ActionResult Details(int id) { try { Sale sale = ctx.Sales.Find(id); SoldPet soldPet = ctx.SoldPets .Include(x => x.Category) .Single(x => x.Id == sale.SoldPetId); return(View(soldPet)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.GetBaseException().ToString()); return(View()); } }
public ActionResult Create(Sale sale) { try { Pet pet = ctx.Pets.Find(sale.PetId); SoldPet soldPet = new SoldPet(pet); ctx.SoldPets.Add(soldPet); ctx.Pets.Remove(pet); ctx.SaveChanges(); int newSoldPetId = ctx.SoldPets.Max(x => x.Id); sale.SoldPetId = newSoldPetId; sale.Id = 0; ctx.Sales.Add(sale); ctx.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Sale creation exception: " + e.GetBaseException().ToString()); return(View()); } }