public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Price,CategoryId,ForSale")] Product product)
        {
            if (id != product.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            var viewmodel = new CreateProductVm();

            viewmodel.Product = product;
            return(View(viewmodel));
        }
        // GET: Products/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var product = await _context.Product.FindAsync(id);

            if (product == null)
            {
                return(NotFound());
            }

            var vm = new CreateProductVm();

            var list = new List <SelectListItem>();

            var listCategories = _context.Category;

            foreach (var category in listCategories)
            {
                list.Add(new SelectListItem
                {
                    Text  = category.Name,
                    Value = category.Id.ToString()
                });
            }

            vm.AllCategories = list;
            vm.Product       = product;

            return(View(vm));
        }
예제 #3
0
        // GET: Products/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var product = await _context.Product.FindAsync(id);

            if (product == null)
            {
                return(NotFound());
            }


            var viewmodel = new CreateProductVm()
            {
                AllCategories = _context.Categories.Select(x => new SelectListItem {
                    Text = x.Name, Value = x.Id.ToString()
                }),
                Product = await _context.Product.FindAsync(id)
            };

            return(View(viewmodel));
        }
        public async Task <IActionResult> Create([Bind("ProductId,Name,ShortDescription,LongDescription,AllergyInformation,Price,IsProductOfTheWeek,InStock,CategoryId,ProfileImage")] CreateProductVm product)
        {
            string ulr = UploadedFile(product);

            if (ModelState.IsValid)
            {
                Product p = new Product
                {
                    ProductId          = product.ProductId,
                    Name               = product.Name,
                    ShortDescription   = product.ShortDescription,
                    LongDescription    = product.LongDescription,
                    AllergyInformation = product.AllergyInformation,
                    Price              = product.Price,
                    IsProductOfTheWeek = product.IsProductOfTheWeek,
                    InStock            = product.InStock,
                    CategoryId         = product.CategoryId,
                    ImageThumbnailUrl  = ulr,
                    ImageUrl           = ulr,
                };
                _context.Add(p);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryId", product.CategoryId);
            return(View(product));
        }
        // GET: AdminProducts/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var product = await _context.Products.FindAsync(id);

            CreateProductVm p = new CreateProductVm
            {
                ProductId          = product.ProductId,
                Name               = product.Name,
                ShortDescription   = product.ShortDescription,
                LongDescription    = product.LongDescription,
                AllergyInformation = product.AllergyInformation,
                Price              = product.Price,
                IsProductOfTheWeek = product.IsProductOfTheWeek,
                InStock            = product.InStock,
                CategoryId         = product.CategoryId,
            };

            if (product == null)
            {
                return(NotFound());
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryId", product.CategoryId);
            return(View(p));
        }
        // GET: Products/Create
        public IActionResult Create()
        {
            CreateProductVm viewmodel = new CreateProductVm
            {
                AllCategories = CategoriesAsSelectListItems
            };

            return(View(viewmodel));
        }
예제 #7
0
        // GET: Products/Create
        public IActionResult Create()
        {
            var viewModel = new CreateProductVm();

            viewModel.AllCategories = _context.Category.Select(category => new SelectListItem()
            {
                Text = category.Name, Value = category.Id.ToString()
            });

            return(View(viewModel));
        }
        // GET: Products/Create
        public IActionResult Create()
        {
            var viewmodel = new CreateProductVm();

            {
                viewmodel.AllCategories = _context.Category.Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.Id.ToString()
                });
            };

            return(View(viewmodel));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Price,CategoryId,ForSale")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }


            var viewmodel = new CreateProductVm();

            viewmodel.Product = product;
            return(View(viewmodel));
        }
        private string UploadedFile(CreateProductVm model)
        {
            string uniqueFileName = null;

            if (model.ProfileImage != null)
            {
                string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.ProfileImage.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.ProfileImage.CopyTo(fileStream);
                }
            }

            return("~/images/" + uniqueFileName);
        }
        public async Task <IActionResult> Edit(int id, [Bind("ProductId,Name,ShortDescription,LongDescription,AllergyInformation,Price,IsProductOfTheWeek,InStock,CategoryId,ProfileImage")] CreateProductVm product)
        {
            if (id != product.ProductId)
            {
                return(NotFound());
            }
            string ulr = UploadedFile(product);

            if (ModelState.IsValid)
            {
                Product p = new Product
                {
                    ProductId          = product.ProductId,
                    Name               = product.Name,
                    ShortDescription   = product.ShortDescription,
                    LongDescription    = product.LongDescription,
                    AllergyInformation = product.AllergyInformation,
                    Price              = product.Price,
                    IsProductOfTheWeek = product.IsProductOfTheWeek,
                    InStock            = product.InStock,
                    CategoryId         = product.CategoryId,
                    ImageThumbnailUrl  = ulr,
                    ImageUrl           = ulr,
                };
                try
                {
                    _context.Update(p);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ProductId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryId", product.CategoryId);
            return(View());
        }
        // GET: Products/Create
        public IActionResult Create()
        {
            var viewmodel = new CreateProductVm();
            List <SelectListItem> test = new List <SelectListItem>();

            var x = _context.Category;

            foreach (var item in x)
            {
                test.Add(new SelectListItem()
                {
                    Text = item.Name, Value = item.Id.ToString()
                });
            }
            viewmodel.AllCategories = test;

            return(View(viewmodel));
        }
        // GET: Products/Create
        public IActionResult Create()
        {
            var vm = new CreateProductVm();

            var list = new List <SelectListItem>();

            var listCategories = _context.Category;

            foreach (var category in listCategories)
            {
                list.Add(new SelectListItem
                {
                    Text  = category.Name,
                    Value = category.Id.ToString()
                });
            }

            vm.AllCategories = list;
            return(View(vm));
        }
예제 #14
0
        public async Task <IActionResult> Create(CreateProductVm vm) // [Bind("Id,Name,Price,CategoryId,ForSale,ProductsRoles")]
        {
            if (ModelState.IsValid)
            {
                vm.Product.ProductsRoles = new List <ProductsRoles>();
                //List<string> xs = new List<string>();
                foreach (var role in vm.SelectedRoles)
                {
                    //xs.Add(role);
                    vm.Product.ProductsRoles.Add(new ProductsRoles {
                        RoleId = role
                    });
                }
                vm.SelectedRoles = new List <string>();
                _context.Add(vm.Product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(null));
        }
예제 #15
0
        // GET: Products/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var product = await _context.Product.FindAsync(id);

            if (product == null)
            {
                return(NotFound());
            }

            var viewModel = new CreateProductVm();

            viewModel.AllCategories = _context.Category.Select(category => new SelectListItem()
            {
                Text = category.Name, Value = category.Id.ToString()
            });
            viewModel.Product = product;

            return(View(viewModel));
        }
예제 #16
0
 public async Task <ActionResult <string> > CreateProducts([FromForm] CreateProductVm ProductRequest, CancellationToken cancellationToken)
 {
     return(Ok(await Mediator.Send(new CreateProductCommand {
         Image = ProductRequest.FileImage, Product = ProductRequest.Product
     }, cancellationToken)));
 }