public async Task <IActionResult> Create(CarBrandEditModel model) { var fileName = Path.GetFileName(ContentDispositionHeaderValue.Parse(model.PathBrand.ContentDisposition).FileName.Value.Trim('"')); var fileExt = Path.GetExtension(fileName); if (!CarBrandsController.AllowedExtensions.Contains(fileExt)) { this.ModelState.AddModelError(nameof(model.PathBrand), "This file type is prohibited"); } if (this.ModelState.IsValid) { var carBrand = new CarBrand { Name = model.Name, Description = model.Description }; var attachmentPath = Path.Combine(this.hostingEnvironment.WebRootPath, "attachments/brands", carBrand.Id.ToString("N") + fileExt); carBrand.PathBrand = $"/attachments/brands/{carBrand.Id:N}{fileExt}"; using (var fileStream = new FileStream(attachmentPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read)) { await model.PathBrand.CopyToAsync(fileStream); } carBrand.Id = Guid.NewGuid(); this._context.Add(carBrand); await this._context.SaveChangesAsync(); return(this.RedirectToAction("Index")); } return(this.View(model)); }
// GET: CarBrands/Edit/5 public async Task <IActionResult> Edit(Guid?id) { if (id == null) { return(this.NotFound()); } var carBrand = await this._context.CarBrands.SingleOrDefaultAsync(m => m.Id == id); if (carBrand == null) { return(this.NotFound()); } var model = new CarBrandEditModel { Name = carBrand.Name, Description = carBrand.Description }; this.ViewBag.CarBrand = carBrand; return(this.View(model)); }