Example #1
0
 public ActionResult Edit(int id)
 {
     if (Session["Id"] != null && Session["UserRank"].ToString() == "Admin")
     {
         Session["New"] = null;
         var automobile = _context.Automobiles.SingleOrDefault(a => a.Id == id);
         if (automobile == null)
         {
             return(HttpNotFound());
         }
         var viewModel = new NewAutomobileViewModel
         {
             Automobile    = automobile,
             NumberOfDoors = _context.NumberOfDoors.ToList(),
             Gearshifts    = _context.Gearshifts.ToList(),
             CarBodies     = _context.CarBodies.ToList()
         };
         Session["UpdateAuto"] = "True";
         return(View("NewAutomobile", viewModel));
     }
     else
     {
         return(HttpNotFound());
     }
 }
Example #2
0
        public ActionResult Save(Automobile automobile, HttpPostedFileBase CarImage)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewAutomobileViewModel
                {
                    Automobile    = new Automobile(),
                    NumberOfDoors = _context.NumberOfDoors.ToList(),
                    CarBodies     = _context.CarBodies.ToList(),
                    Gearshifts    = _context.Gearshifts.ToList(),
                };
                Session["CarMessage"] = null;
                return(View("NewAutomobile", viewModel));
            }
            if (automobile.Id == 0)
            {
                if (CarImage != null)
                {
                    string filename = Path.GetFileName(CarImage.FileName);
                    automobile.ImagePath = "/Images/" + filename;
                    filename             = Path.Combine(Server.MapPath("~/Images/"), filename);
                    CarImage.SaveAs(filename);
                }
                else
                {
                    automobile.ImagePath = "/Images/template-car.jpg";
                }
                _context.Automobiles.Add(automobile);
                Session["CarMessage"] = "Auto successfully added.";
            }
            else
            {
                var automobileInDb = _context.Automobiles
                                     .Include(a => a.NumberOfDoor)
                                     .Include(a => a.CarBody)
                                     .Include(a => a.Gearshift)
                                     .Single(a => a.Id == automobile.Id);

                automobileInDb.CarBrand       = automobile.CarBrand;
                automobileInDb.CarModel       = automobile.CarModel;
                automobileInDb.ProductYear    = automobile.ProductYear;
                automobileInDb.Cubicase       = automobile.Cubicase;
                automobileInDb.NumberOfDoorId = automobile.NumberOfDoorId;
                automobileInDb.CarBodyId      = automobile.CarBodyId;
                automobileInDb.GearshiftId    = automobile.GearshiftId;
                if (CarImage != null)
                {
                    string filename = Path.GetFileName(CarImage.FileName);
                    automobileInDb.ImagePath = "/Images/" + filename;
                    filename = Path.Combine(Server.MapPath("~/Images/"), filename);
                    CarImage.SaveAs(filename);
                }
                Session["CarMessage"] = "Auto successfully updated.";
            }
            _context.SaveChanges();
            return(RedirectToAction("New", "Automobiles"));
        }
Example #3
0
 public ActionResult New()
 {
     if (Session["Id"] != null && Session["UserRank"].ToString() == "Admin")
     {
         var viewModel = new NewAutomobileViewModel
         {
             Automobile    = new Automobile(),
             NumberOfDoors = _context.NumberOfDoors.ToList(),
             Gearshifts    = _context.Gearshifts.ToList(),
             CarBodies     = _context.CarBodies.ToList()
         };
         Session["New"] = "New";
         return(View("NewAutomobile", viewModel));
     }
     else
     {
         return(HttpNotFound());
     }
 }