// Get one; notice the name of the method public ManufacturerBase GetManufacturerById(int id) { // Fetch the object using its identifier // A DbSet collection supports the spiffy Find() method // Way easier syntax than SingleOrDefault(), // because it doesn't need a lambda expression var fetchedObject = ds.Manufacturers.Find(id); if (fetchedObject == null) { // Return null to the caller (who will also test the result) return(null); } else { // Create and deliver a view model object var man = new ManufacturerBase(); man.Id = fetchedObject.Id; man.Name = fetchedObject.Name; man.Country = fetchedObject.Country; man.YearStarted = fetchedObject.YearStarted; return(man); } }
// ############################################################ // Edit existing public ManufacturerBase EditManufacturer(ManufacturerBase newItem) { // Attempt to fetch the object with the matching identifier var fetchedObject = ds.Manufacturers.Find(newItem.Id); if (fetchedObject == null) { // Return null to the caller (who will also test the result) return(null); } else { // Update the object with the incoming values // Before doing this, we may have to perform // business-rule validations ds.Entry(fetchedObject).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare the object to be returned var editedItem = new ManufacturerBase(); editedItem.Id = fetchedObject.Id; editedItem.Name = fetchedObject.Name; editedItem.Country = fetchedObject.Country; editedItem.YearStarted = fetchedObject.YearStarted; // Return the result return(editedItem); } }
public ActionResult Create(ManufacturerAdd newItem) { if (ModelState.IsValid) { ManufacturerBase addedItem = m.AddManufacturer(newItem); // Should probably do a quick if-null test return(RedirectToAction("details", new { id = addedItem.Id })); } else { return(RedirectToAction("index")); } }
// ############################################################ // // GET: /Manufacturers/Edit/5 public ActionResult Edit(int id) { // Attempt to get the object with the matching identifier ManufacturerBase fetchedObject = m.GetManufacturerById(id); if (fetchedObject == null) { return(RedirectToAction("index")); } else { return(View(fetchedObject)); } }
public ActionResult Edit(int id, ManufacturerBase newItem) { // Two tests are required... if (ModelState.IsValid & id == newItem.Id) { // Attempt to update the item ManufacturerBase editedItem = m.EditManufacturer(newItem); if (editedItem == null) { return(RedirectToAction("index")); } else { return(RedirectToAction("details", new { id = editedItem.Id })); } } else { return(RedirectToAction("index")); } }
// ############################################################ // Add new public ManufacturerBase AddManufacturer(ManufacturerAdd newItem) { // Create a design model object Manufacturer man = new Manufacturer(); man.Name = newItem.Name; man.Country = newItem.Country; man.YearStarted = newItem.YearStarted; // Add and save ds.Manufacturers.Add(man); ds.SaveChanges(); // Prepare the object to be returned ManufacturerBase addedItem = new ManufacturerBase(); addedItem.Id = man.Id; addedItem.Name = man.Name; addedItem.Country = man.Country; addedItem.YearStarted = man.YearStarted; // Return the result return addedItem; }
// ############################################################ // Add new public ManufacturerBase AddManufacturer(ManufacturerAdd newItem) { // Create a design model object Manufacturer man = new Manufacturer(); man.Name = newItem.Name; man.Country = newItem.Country; man.YearStarted = newItem.YearStarted; // Add and save ds.Manufacturers.Add(man); ds.SaveChanges(); // Prepare the object to be returned ManufacturerBase addedItem = new ManufacturerBase(); addedItem.Id = man.Id; addedItem.Name = man.Name; addedItem.Country = man.Country; addedItem.YearStarted = man.YearStarted; // Return the result return(addedItem); }
public IEnumerable <ManufacturerBase> GetAllManufacturers() { // Fetch from the persistent store var fetchedObjects = ds.Manufacturers.OrderBy(man => man.Name); // Prepare the view model objects... // Create a collection var manufacturers = new List <ManufacturerBase>(); // Go through the fetch results foreach (var item in fetchedObjects) { var man = new ManufacturerBase(); man.Id = item.Id; man.Name = item.Name; man.Country = item.Country; man.YearStarted = item.YearStarted; manufacturers.Add(man); } // Return the result return(manufacturers); }
// ############################################################ // Edit existing public ManufacturerBase EditManufacturer(ManufacturerBase newItem) { // Attempt to fetch the object with the matching identifier var fetchedObject = ds.Manufacturers.Find(newItem.Id); if (fetchedObject == null) { // Return null to the caller (who will also test the result) return null; } else { // Update the object with the incoming values // Before doing this, we may have to perform // business-rule validations ds.Entry(fetchedObject).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare the object to be returned var editedItem = new ManufacturerBase(); editedItem.Id = fetchedObject.Id; editedItem.Name = fetchedObject.Name; editedItem.Country = fetchedObject.Country; editedItem.YearStarted = fetchedObject.YearStarted; // Return the result return editedItem; } }
// Get one; notice the name of the method public ManufacturerBase GetManufacturerById(int id) { // Fetch the object using its identifier // A DbSet collection supports the spiffy Find() method // Way easier syntax than SingleOrDefault(), // because it doesn't need a lambda expression var fetchedObject = ds.Manufacturers.Find(id); if (fetchedObject == null) { // Return null to the caller (who will also test the result) return null; } else { // Create and deliver a view model object var man = new ManufacturerBase(); man.Id = fetchedObject.Id; man.Name = fetchedObject.Name; man.Country = fetchedObject.Country; man.YearStarted = fetchedObject.YearStarted; return man; } }
public IEnumerable<ManufacturerBase> GetAllManufacturers() { // Fetch from the persistent store var fetchedObjects = ds.Manufacturers.OrderBy(man => man.Name); // Prepare the view model objects... // Create a collection var manufacturers = new List<ManufacturerBase>(); // Go through the fetch results foreach (var item in fetchedObjects) { var man = new ManufacturerBase(); man.Id = item.Id; man.Name = item.Name; man.Country = item.Country; man.YearStarted = item.YearStarted; manufacturers.Add(man); } // Return the result return manufacturers; }
public ActionResult Edit(int id, ManufacturerBase newItem) { // Two tests are required... if (ModelState.IsValid & id == newItem.Id) { // Attempt to update the item ManufacturerBase editedItem = m.EditManufacturer(newItem); if (editedItem == null) { return RedirectToAction("index"); } else { return RedirectToAction("details", new { id = editedItem.Id }); } } else { return RedirectToAction("index"); } }