コード例 #1
0
        public ActionResult AddVehicle()
        {
            var model = new VehicleViewModel();

            model         = model.GetLists();
            model.vehicle = new Vehicle();
            model.UserId  = GetUserId();
            return(View(model));
        }
コード例 #2
0
        public ActionResult AddVehicle(VehicleViewModel viewModel)
        {
            var vehicleRepo = VehicleFactory.GetVehicleRepository();

            try
            {
                if (ModelState.IsValid)
                {
                    viewModel.vehicle.UserId = GetUserId();

                    if (viewModel.ImageUpload != null && viewModel.ImageUpload.ContentLength > 0)
                    {
                        var savepath = Server.MapPath("~/Images");


                        string fileName  = Path.GetFileNameWithoutExtension(viewModel.ImageUpload.FileName);
                        string extension = Path.GetExtension(viewModel.ImageUpload.FileName);
                        var    filePath  = Path.Combine(savepath, fileName + extension);

                        filePath = Path.Combine(savepath, "inventory-" + viewModel.vehicle.VehicleId + extension);

                        WebImage img = new WebImage(viewModel.ImageUpload.InputStream);
                        img.Resize(150, 100, preserveAspectRatio: false, preventEnlarge: true).Crop(1, 1);
                        img.Save(filePath);

                        viewModel.vehicle.PictureFileName = Path.GetFileName(filePath);
                    }

                    vehicleRepo.InsertVehicle(viewModel.vehicle);
                    var param = new VehicleSearchParameters();
                    viewModel.vehicle.VehicleId = vehicleRepo.GetSearchResults(param).Count();
                }

                else
                {
                    viewModel         = viewModel.GetLists();
                    viewModel.UserId  = GetUserId();
                    viewModel.vehicle = new Vehicle();

                    return(View(viewModel));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(RedirectToAction("EditVehicle", new { id = viewModel.vehicle.VehicleId }));
        }