Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("CategoryId,Name,Description")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("CafeId,Address,OpenHour,CloseHour")] Cafe cafe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cafe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(cafe));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("DishId,Name,CategoryId,Price,Weight,Description,ImageUrl")] Dish dish)
        {
            if (ModelState.IsValid)
            {
                _context.Add(dish);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "Name", dish.CategoryId);
            return(View(dish));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                    {
                        await fileExcel.CopyToAsync(stream);

                        try
                        {
                            using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                            {
                                foreach (IXLWorksheet worksheet in workBook.Worksheets)
                                {
                                    Category newcat;
                                    var      c = (from cat in _context.Categories
                                                  where cat.Name.Contains(worksheet.Name)
                                                  select cat).ToList();
                                    if (c.Count > 0)
                                    {
                                        newcat = c[0];
                                    }
                                    else
                                    {
                                        newcat             = new Category();
                                        newcat.Name        = worksheet.Name;
                                        newcat.Description = "from EXCEL";

                                        _context.Categories.Add(newcat);
                                    }

                                    foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                    {
                                        try
                                        {
                                            Dish dish = new Dish();
                                            dish.Name        = row.Cell(1).Value.ToString();
                                            dish.Price       = Convert.ToInt32(row.Cell(2).Value.ToString());
                                            dish.Weight      = Convert.ToInt32(row.Cell(3).Value.ToString());
                                            dish.Description = row.Cell(4).Value.ToString();
                                            dish.ImageUrl    = row.Cell(5).Value.ToString();
                                            dish.Category    = newcat;

                                            var results = new List <ValidationResult>();
                                            var context = new ValidationContext(dish, null, null);
                                            if (Validator.TryValidateObject(dish, context, results, true))
                                            {
                                                _context.Dishes.Add(dish);
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                            await Response.WriteAsync("<script>window.location = 'index';alert('Uploaded file of unsupported format!');</script>");
                        }
                    }
                }

                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }