Example #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,ImageFile,taste,price")] TblProduct tblProduct)
        {
            if (ModelState.IsValid)
            {
                //Save image to wwwroot/image
                string wwwRootPath = _hostEnvironment.WebRootPath;
                string fileName    = Path.GetFileNameWithoutExtension(tblProduct.ImageFile.FileName);
                string extension   = Path.GetExtension(tblProduct.ImageFile.FileName);
                tblProduct.ImageName = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                string path = Path.Combine(wwwRootPath + "/Image/", fileName);
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await tblProduct.ImageFile.CopyToAsync(fileStream);
                }

                if (_context.TblProduct.Any(ac => ac.Name.Equals(tblProduct.Name)))
                {
                    ModelState.AddModelError("Name", "Name already exists.");
                }
                else
                {
                    _context.Add(tblProduct);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(tblProduct));
        }
Example #2
0
        public async Task <IActionResult> Create([Bind("ID,Name,Time,Difficulty,NumberOfLikes,Ingredients,Process,TipsAndTricks")] Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recipe);
                await _context.SaveChangesAsync();

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