AssocAddEdit.Controllers.Manager.VehicleAdd C# (CSharp) Метод

VehicleAdd() публичный Метод

public VehicleAdd ( VehicleAdd newItem ) : VehicleWithDetail
newItem VehicleAdd
Результат VehicleWithDetail
        public VehicleWithDetail VehicleAdd(VehicleAdd newItem)
        {
            // This method is called from the Vehicles controller...
            // ...AND the Manufacturers controller

            // When adding an object with a required to-one association,
            // MUST fetch the associated object first

            // Attempt to find the associated object
            var a = ds.Manufacturers.Find(newItem.ManufacturerId);

            if (a == null)
            {
                return null;
            }
            else
            {
                // Attempt to add the new item
                var addedItem = ds.Vehicles.Add(Mapper.Map<Vehicle>(newItem));
                // Set the associated item property
                addedItem.Manufacturer = a;
                ds.SaveChanges();

                return (addedItem == null) ? null : Mapper.Map<VehicleWithDetail>(addedItem);
            }
        }

Usage Example

        public ActionResult Create(VehicleAdd newItem)
        {
            // Validate the input
            if (!ModelState.IsValid)
            {
                return(View(newItem));
            }

            // Process the input
            var addedItem = m.VehicleAdd(newItem);

            if (addedItem == null)
            {
                return(View(newItem));
            }
            else
            {
                return(RedirectToAction("details", new { id = addedItem.Id }));
            }
        }
All Usage Examples Of AssocAddEdit.Controllers.Manager::VehicleAdd