Пример #1
0
        public IActionResult Create(CreateCarViewModel modelvm)
        {
            if (ModelState.IsValid)
            {
                int id_cl   = _context.FindUser(User.Identity.Name).id_client.Value;
                int id_supp = _context.FindSupplierByClient(id_cl).Id;
                Car car     = new Car()
                {
                    Mark             = modelvm.Mark,
                    Model            = modelvm.Model,
                    Color            = modelvm.Color,
                    Goverment_number = modelvm.Goverment_number,
                    Year             = modelvm.Year,
                    id_supplier      = id_supp,
                    Price            = modelvm.Price,
                    status           = "Свободен",
                    country          = modelvm.country,
                    city             = modelvm.city,
                    Image            = null,
                    ImageMimeType    = null,
                };

                if (modelvm.Image != null)
                {
                    byte[] imageData = null;
                    using (var binaryReader = new BinaryReader(modelvm.Image.OpenReadStream()))
                    {
                        imageData = binaryReader.ReadBytes((int)modelvm.Image.Length);
                    }
                    car.Image         = Convert.ToBase64String(imageData);;
                    car.ImageMimeType = modelvm.Image.ContentType;
                }
                if (_context.AddCar(car))
                {
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Ошибка");
                }
            }
            else
            {
                ModelState.AddModelError("", "Ошибка");
            }
            if (AllMarks[0] == "Все")
            {
                AllMarks.RemoveAt(0);
            }
            ViewBag.Marks = new SelectList(AllMarks);
            return(View(modelvm));
        }