Exemple #1
0
        public IActionResult Create(BrandViewModel bvm)
        {
            Brand person = new Brand {
                BrandName   = bvm.BrandName, BicycleModels = bvm.BicycleModels, Country = bvm.Country,
                CountryId   = bvm.CountryId, Dealer = bvm.Dealer, DealerId = bvm.DealerId,
                Description = bvm.Description
            };

            if (bvm.Image != null)
            {
                byte[] imageData = null;
                using (var binaryReader = new BinaryReader(bvm.Image.OpenReadStream()))
                {
                    imageData = binaryReader.ReadBytes((int)bvm.Image.Length);
                }
                person.Image = imageData;
            }
            _context.Brands.Add(person);
            _context.SaveChanges();
            return(RedirectToAction(nameof(Index)));
            //return View(person);
        }
        public IActionResult Create(BicycleModelViewModel bvm)
        {
            BicycleModel bicycleModel = new BicycleModel
            {
                BrandId    = bvm.BrandId,
                CategoryId = bvm.CategoryId,
                GenderId   = bvm.GenderId,

                Description     = bvm.Description,
                Id              = bvm.Id,
                ModelName       = bvm.ModelName,
                ModelYear       = bvm.ModelYear,
                Price           = bvm.Price,
                SizeColorModels = bvm.SizeColorModels
            };

            if (ModelState.IsValid)
            {
                if (bvm.Image != null)
                {
                    byte[] imageData = null;
                    using (var binaryReader = new BinaryReader(bvm.ImageFile.OpenReadStream()))
                    {
                        imageData = binaryReader.ReadBytes((int)bvm.Image.Length);
                    }
                    bicycleModel.Image = imageData;
                }
                _context.Add(bicycleModel);
                _context.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BrandId"]    = new SelectList(_context.Brands, "Id", "BrandName", bicycleModel.BrandId);
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "CategoryName", bicycleModel.CategoryId);
            ViewData["GenderId"]   = new SelectList(_context.Genders, "Id", "GenderName", bicycleModel.GenderId);
            return(View(bicycleModel));
        }