public ActionResult Create([Bind(Include = "Id,NumberOfDoors,NumberOfWheels,BodyTypeId,VehicleTypeId,Make,Model,Engine")] Car car)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CarBodyTypeViewModel(car)
                {
                    BodyTypes = _context.BodyTypes.ToList()
                };
                return(View("Create", viewModel));
            }

            if (car.Id == 0)
            {
                _context.Cars.Add(car);
            }
            else
            {
                var carInDb = _context.Cars.Single(model => model.Id == car.Id);

                carInDb.Make           = car.Make;
                carInDb.Model          = car.Model;
                carInDb.Engine         = car.Engine;
                carInDb.BodyType       = car.BodyType;
                carInDb.BodyTypeId     = car.BodyTypeId;
                carInDb.NumberOfDoors  = car.NumberOfDoors;
                carInDb.NumberOfWheels = car.NumberOfWheels;
            }
            _context.SaveChanges();

            var item = new CarBodyTypeViewModel(car);

            return(RedirectToAction("Details", new { id = item.Id }));

            //return RedirectToAction("Index");
        }
Beispiel #2
0
 public ActionResult Create(CarBodyTypeViewModel model)
 {
     if (ModelState.IsValid)
     {
         _carBodyTypeManager.Add(_carBodyTypeFieldCopier.CopyFields(model, new CarBodyType()));
         return(RedirectToAction("List"));
     }
     return(View(model));
 }
        // GET: Cars/Create
        public ActionResult Create()
        {
            var bodyTypes = _context.BodyTypes.ToList();
            var viewModel = new CarBodyTypeViewModel
            {
                BodyTypes = bodyTypes
            };

            return(View(viewModel));
        }
Beispiel #4
0
 public CarBodyTypeViewModel CopyFields(CarBodyType from, CarBodyTypeViewModel to)
 {
     if (to == null)
     {
         throw new NullReferenceException();
     }
     if (from == null)
     {
         throw new NullReferenceException();
     }
     to.Id   = from.Id;
     to.Name = from.Name;
     return(to);
 }
Beispiel #5
0
 public ActionResult Edit(CarBodyTypeViewModel model)
 {
     if (ModelState.IsValid)
     {
         var dbItem = _carBodyTypeManager.GetById(model.Id);
         if (dbItem == null)
         {
             throw new NullReferenceException();
         }
         dbItem = _carBodyTypeFieldCopier.CopyFields(model, dbItem);
         _carBodyTypeManager.Edit(dbItem);
         return(RedirectToAction("List"));
     }
     return(View(model));
 }
Beispiel #6
0
 public JsonResult CheckBodyTypeNameForExist(CarBodyTypeViewModel model)
 {
     model.Name = model.Name.TrimStart(' ');
     model.Name = model.Name.TrimEnd(' ');
     return(Json(_carBodyTypeManager.GetAll().Where(c => c.Name == model.Name && c.Id != model.Id).ToList().Count <= 0, JsonRequestBehavior.AllowGet));
 }