public ActionResult Create(ProductAdd newItem) { // Model state validity check // If not valid, redirect to the index view // Otherwise, continue // Attempt to add the new item, using the manager method // If the attempt fails, redirect to the index view // Otherwise, redirect to the details view for the added object ProductBase addedItem = null; // Check that the incoming data is valid if (ModelState.IsValid) { addedItem = m.AddNewProduct(newItem); } else { // Return the object so the user can edit it correctly return(View(newItem)); } // If the incoming data is valid and the new data was added, redirect return(RedirectToAction("index")); }
public ProductBase AddNewProduct(ProductAdd newItem) { // Calculate the next value for the identifier int newId = (ds.Products.Count() > 0) ? newId = ds.Products.Max(id => id.Id) + 1 : 1; var supplier = ds.Suppliers.Find(newItem.SupplierId); Mapper.CreateMap <ProductAdd, Product>(); // Create a new item; notice the property mapping var addedItem = new Product { Id = newId, Supplier = supplier //MSRP = newItem.MSRP, //Name = newItem.Name, //ProductId = newItem.ProductId, //Size = newItem.Size, //SupplierId = newItem.SupplierId, //UPC = newItem.UPC }; var addItem = new ProductBase { Id = addedItem.Id, SupplierName = supplier.Name, MSRP = newItem.MSRP, Name = newItem.Name, ProductId = newItem.ProductId, Size = newItem.Size, SupplierId = newItem.SupplierId, UPC = newItem.UPC }; // Add the new item to the store ds.Products.Add(addedItem); // Return the new item return(Mapper.Map <ProductBase>(addItem)); }
public ProductBase GetProductById(int id) { // Fetch from the data store Product fetchedObject = ds.Products.Include("Supplier").Single(i => i.Id == id); ProductBase result = null; // If not found, return null if (fetchedObject == null) { return(null); } else // Otherwise, continue { // Prepare the return result result = Mapper.Map <ProductBase>(fetchedObject); result.SupplierName = fetchedObject.Supplier.Name; } // Return the result return(result); }