Ejemplo n.º 1
0
        public ActionResult Save(Moto moto, HttpPostedFileBase image)
        {
            if (!ModelState.IsValid)
            {
                // Daca modelul nu e valid ne intoarcem pe pagina
                var viewModel = new MotoFormViewModel
                {
                    Title = CarFormViewmodel.Add,
                    Moto  = moto
                };

                if (moto.Id > 0)
                {
                    viewModel.Title = CarFormViewmodel.Edit;
                }

                return(View("Form", viewModel));
            }

            if (image != null)
            {
                string imagePath = "~\\Content\\Images\\" + image.FileName;
                image.SaveAs(HttpContext.Server.MapPath(imagePath));
                moto.Image = imagePath;
            }

            if (moto.Image != null && moto.Image[0] != '~')
            {
                string path = "~\\Content\\Images\\" + moto.Image.ToString();
                moto.Image = path;
            }

            if (moto.Id == 0)
            {
                // Daca masina este nou adaugata o introducem in Db
                _context.Moto.Add(moto);
            }
            else
            {
                // Daca masina este editata o updatam in Db
                var motoInDb = _context.Moto.Single(m => m.Id == moto.Id);
                motoInDb.Id                  = moto.Id;
                motoInDb.Manufacturer        = moto.Manufacturer;
                motoInDb.Series              = moto.Series;
                motoInDb.Vin                 = moto.Vin;
                motoInDb.CylindricalCapacity = moto.CylindricalCapacity;
                motoInDb.HorsePower          = moto.HorsePower;
                motoInDb.YearManufactured    = moto.YearManufactured;
                motoInDb.Caroserie           = moto.Caroserie;
                motoInDb.Image               = "~\\Content\\Images\\" + moto.Image;
                motoInDb.ExtraDetails        = moto.ExtraDetails;
            }

            _context.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int id)
        {
            var moto = _context.Moto.SingleOrDefault(m => m.Id == id);

            if (moto == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new MotoFormViewModel
            {
                Title = CarFormViewmodel.Edit,
                Moto  = moto
            };

            return(View("Form", viewModel));
        }
Ejemplo n.º 3
0
        public ActionResult Add()
        {
            var viewModel = new MotoFormViewModel(0);

            return(View("Form", viewModel));
        }