Example #1
0
        public IActionResult Create()
        {
            CreateGoodView model = new CreateGoodView
            {
                Producers = unitOfWork.Producers.GetAll().ToList(),
                Storages  = unitOfWork.Storages.GetAll().ToList()
            };

            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> Create(CreateGoodView model, List <string> storages)
        {
            if (ModelState.IsValid)
            {
                Good good = new Good
                {
                    Name              = model.Name,
                    Specification     = model.Specification,
                    PhotoUrl          = model.PhotoUrl,
                    YearOfManufacture = model.YearOfManufacture,
                    WarrantyTerm      = model.WarrantyTerm,
                    Producer          = unitOfWork.Producers.GetAll().Where(p => p.Name == Request.Form["producerSelect"]).First(),
                    Price             = model.Price,
                    Type              = model.Type,
                    Count             = model.Count,
                };

                if (storages.Count > 0)
                {
                    foreach (var storage in storages)
                    {
                        Storage tempStorage = unitOfWork.Storages.GetAll().Where(s => s.Street == storage).First();
                        good.Storages.Add(new GoodStorage()
                        {
                            Good = good, Storage = tempStorage
                        });
                    }
                }

                await unitOfWork.Goods.Create(good);

                await unitOfWork.SaveAsync();

                HttpContext.Session.Set("goods", unitOfWork.Goods.GetAll().ToList());

                return(RedirectToAction("Index", "Good"));
            }

            return(View(model));
        }