public async Task <IActionResult> Edit(Guid id, CarGradeEditModel model) { if (id == null) { return(this.NotFound()); } var carGrade = await this._context.CarGrades .SingleOrDefaultAsync(m => m.Id == id); if (carGrade == null) { return(this.NotFound()); } if (this.ModelState.IsValid) { carGrade.Name = model.Name; carGrade.Description = model.Description; await this._context.SaveChangesAsync(); return(this.RedirectToAction("Details", "CarModels", new { carModelId = carGrade.CarModelId })); } ViewData["CarModelId"] = new SelectList(this._context.CarModels, "Id", "ModelPath", carGrade.CarModelId); return(this.View(model)); }
public async Task <IActionResult> Create(Guid?carModelId, CarGradeEditModel model) { if (carModelId == null) { this.NotFound(); } var fileName = Path.GetFileName(ContentDispositionHeaderValue.Parse(model.GradePath.ContentDisposition).FileName.Value.Trim('"')); var fileExt = Path.GetExtension(fileName); if (!CarGradesController.AllowedExtensions.Contains(fileExt)) { this.ModelState.AddModelError(nameof(model.GradePath), "This file type is prohibited"); } var carModel = await this._context.CarModels .SingleOrDefaultAsync(m => m.Id == carModelId); if (carModel == null) { this.NotFound(); } if (this.ModelState.IsValid) { var carGrade = new CarGrade { CarModelId = carModel.Id, Name = model.Name, Description = model.Description }; var attachmentPath = Path.Combine(this.hostingEnvironment.WebRootPath, "attachments/grades", carGrade.Id.ToString("N") + fileExt); carGrade.GradePath = $"/attachments/grades/{carGrade.Id:N}{fileExt}"; using (var fileStream = new FileStream(attachmentPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read)) { await model.GradePath.CopyToAsync(fileStream); } carGrade.Id = Guid.NewGuid(); this._context.Add(carGrade); await this._context.SaveChangesAsync(); return(this.RedirectToAction("Details", "CarModels", new { carModelId = carModel.Id })); } this.ViewData["CarModelId"] = new SelectList(_context.CarModels, "Id", "ModelPath"); this.ViewBag.CarModels = carModel; return(this.View(model)); }