示例#1
0
        public async Task <IActionResult> Edit(Guid id, CarPartEditModel model)
        {
            if (id == null)
            {
                return(this.NotFound());
            }

            var carPart = await this._context.CarParts
                          .SingleOrDefaultAsync(m => m.Id == id);

            if (carPart == null)
            {
                return(this.NotFound());
            }
            if (this.ModelState.IsValid)
            {
                carPart.Name        = model.Name;
                carPart.Description = model.Description;

                await this._context.SaveChangesAsync();

                return(this.RedirectToAction("Details", "CarGrades", new { carGradeId = carPart.CarGradeId }));
            }
            this.ViewData["CarGradeId"] = new SelectList(this._context.CarGrades, "Id", "GradePath", carPart.CarGradeId);
            return(this.View(model));
        }
示例#2
0
        public async Task <IActionResult> Create(Guid?carGradeId, CarPartEditModel model)
        {
            if (carGradeId == null)
            {
                this.NotFound();
            }

            var fileName = Path.GetFileName(ContentDispositionHeaderValue.Parse(model.PartPath.ContentDisposition).FileName.Value.Trim('"'));
            var fileExt  = Path.GetExtension(fileName);

            if (!CarPartsController.AllowedExtensions.Contains(fileExt))
            {
                this.ModelState.AddModelError(nameof(model.PartPath), "This file type is prohibited");
            }

            var carGrade = await this._context.CarGrades
                           .SingleOrDefaultAsync(m => m.Id == carGradeId);

            if (carGrade == null)
            {
                this.NotFound();
            }


            if (this.ModelState.IsValid)
            {
                var carPart = new CarPart
                {
                    CarGradeId  = carGrade.Id,
                    Name        = model.Name,
                    Description = model.Description
                };

                var attachmentPath = Path.Combine(this.hostingEnvironment.WebRootPath, "attachments/parts", carPart.Id.ToString("N") + fileExt);
                carPart.PartPath = $"/attachments/parts/{carPart.Id:N}{fileExt}";
                using (var fileStream = new FileStream(attachmentPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read))
                {
                    await model.PartPath.CopyToAsync(fileStream);
                }

                carPart.Id = Guid.NewGuid();
                this._context.Add(carPart);
                await this._context.SaveChangesAsync();

                return(this.RedirectToAction("Details", "CarGrades", new { carGradeId = carGrade.Id }));
            }
            this.ViewData["CarGradeId"] = new SelectList(this._context.CarGrades, "Id", "GradePath");
            this.ViewBag.CarGrades      = carGrade;
            return(this.View(model));
        }