예제 #1
0
        public async Task <IActionResult> Create([Bind("Vehicle_ID,Owners_Name,Manufacturer,Year_of_Manufacture,Weight")] Vehicle vehicle)
        {
            if (ModelState.IsValid)
            {
                var cat = await GetCategory(vehicle.Weight);

                vehicle.Category_ID = cat.Category_ID;
                _context.Add(vehicle);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vehicle));
        }
        public async Task <IActionResult> PutCategory(int id)
        {
            Category  origianCategory = _context.Categories.Find(id);
            var       dict            = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString());
            IFormFile file;

            byte[] photo;
            string ImageMimeType;

            if (Request.Form.Files.Count != 0)
            {
                file  = Request.Form.Files[0];
                photo = await GetByteArrayFromImageAsync(Request.Form.Files[0]);

                ImageMimeType = file.ContentType;
            }
            else
            {
                photo         = origianCategory.PhotoFile;
                ImageMimeType = origianCategory.ImageMimeType;
            }

            var name = dict["Name"];


            Category category = new Category()
            {
                Category_ID   = int.Parse(dict["Category_ID"]),
                Name          = dict["Name"],
                Min           = int.Parse(dict["Min"]),
                Max           = int.Parse(dict["Max"]),
                PhotoFile     = photo,
                ImageMimeType = ImageMimeType
            };

            if (id != category.Category_ID)
            {
                return(BadRequest());
            }

            //_context.Entry(category).State = EntityState.Modified;
            _context.Entry(origianCategory).CurrentValues.SetValues(category);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }