예제 #1
0
        // ############################################################
        // Add new

        public VehicleBase AddNewVehicle(VehicleAdd newItem)
        {
            // Attempt to fetch the associated object
            var m = ds.Manufacturers.Find(newItem.ManufacturerId);

            if (m == null)
            {
                return(null);
            }
            else
            {
                // Associated object is valid, so continue

                // Create a new design model object
                var addedItem = new Vehicle()
                {
                    Model        = newItem.Model,
                    Trim         = newItem.Trim,
                    ModelYear    = newItem.ModelYear,
                    MSRP         = newItem.MSRP,
                    Manufacturer = m
                };

                // Add and save
                ds.Vehicles.Add(addedItem);
                ds.SaveChanges();

                // Prepare the return object
                var v = new VehicleBase()
                {
                    Id               = addedItem.Id,
                    Model            = addedItem.Model,
                    Trim             = addedItem.Trim,
                    ModelYear        = addedItem.ModelYear,
                    MSRP             = addedItem.MSRP,
                    ManufacturerId   = m.Id,
                    ManufacturerName = m.Name
                };

                // Return the result
                return(v);
            }
        }
예제 #2
0
        // ############################################################
        // Add new
        public VehicleBase AddNewVehicle(VehicleAdd newItem)
        {
            // Attempt to fetch the associated object
            var m = ds.Manufacturers.Find(newItem.ManufacturerId);

            if (m == null)
            {
                return null;
            }
            else
            {
                // Associated object is valid, so continue

                // Create a new design model object
                var addedItem = new Vehicle()
                {
                    Model = newItem.Model,
                    Trim = newItem.Trim,
                    ModelYear = newItem.ModelYear,
                    MSRP = newItem.MSRP,
                    Manufacturer = m
                };

                // Add and save
                ds.Vehicles.Add(addedItem);
                ds.SaveChanges();

                // Prepare the return object
                var v = new VehicleBase()
                {
                    Id = addedItem.Id,
                    Model = addedItem.Model,
                    Trim = addedItem.Trim,
                    ModelYear = addedItem.ModelYear,
                    MSRP = addedItem.MSRP,
                    ManufacturerId = m.Id,
                    ManufacturerName = m.Name
                };

                // Return the result
                return v;
            }
        }
        public ActionResult Create(VehicleAdd newItem)
        {
            if (ModelState.IsValid)
            {
                // Add the new object
                var addedItem = m.AddNewVehicle(newItem);

                if (addedItem == null)
                {
                    return RedirectToAction("index");
                }
                else
                {
                    return RedirectToAction("details", new { Id = addedItem.Id });
                }
            }
            else
            {
                return RedirectToAction("index");
            }
        }
예제 #4
0
        public ActionResult Create(VehicleAdd newItem)
        {
            if (ModelState.IsValid)
            {
                // Add the new object
                var addedItem = m.AddNewVehicle(newItem);

                if (addedItem == null)
                {
                    return(RedirectToAction("index"));
                }
                else
                {
                    return(RedirectToAction("details", new { Id = addedItem.Id }));
                }
            }
            else
            {
                return(RedirectToAction("index"));
            }
        }