public async Task <IActionResult> Edit(int id, int IdFactory, [Bind("Id,IdGarbageType,IdFactory")] FactoryGarbageType factoryGarbageType)
        {
            if (id != factoryGarbageType.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(factoryGarbageType);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FactoryGarbageTypeExists(factoryGarbageType.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index", "FactoryGarbageTypes", new { id = IdFactory }));
            }
            ViewData["IdFactory"]     = new SelectList(_context.Factory, "Id", "Name", factoryGarbageType.IdFactory);
            ViewData["IdGarbageType"] = new SelectList(_context.GarbageType, "Id", "Name", factoryGarbageType.IdGarbageType);
            return(RedirectToAction("Index", "FactoryGarbageTypes", new { id = IdFactory }));;
        }
        public async Task <IActionResult> Create(int IdFactory, [Bind("Id,IdGarbageType,IdFactory")] FactoryGarbageType factoryGarbageType)
        {
            factoryGarbageType.IdFactory = IdFactory;
            if (ModelState.IsValid)
            {
                _context.Add(factoryGarbageType);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "FactoryGarbageTypes", new { id = IdFactory }));
            }
            ViewData["IdFactory"]     = new SelectList(_context.Factory, "Id", "Name", factoryGarbageType.IdFactory);
            ViewData["IdGarbageType"] = new SelectList(_context.GarbageType, "Id", "Name", factoryGarbageType.IdGarbageType);
            return(View(factoryGarbageType));
        }
예제 #3
0
        // [ValidateAntiForgeryToken]
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            int errorCount = 0;

            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                    {
                        await fileExcel.CopyToAsync(stream);

                        using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                        {
                            foreach (IXLWorksheet worksheet in workBook.Worksheets)
                            {
                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    try
                                    {
                                        Factory factory;
                                        var     f = (from fab in _context.Factory
                                                     where fab.Name.Contains(row.Cell(1).Value.ToString())
                                                     select fab).ToList();
                                        if (f.Count > 0)
                                        {
                                            factory = f[0];
                                        }
                                        else
                                        {
                                            factory         = new Factory();
                                            factory.Name    = row.Cell(1).Value.ToString();
                                            factory.Website = row.Cell(2).Value.ToString();
                                            factory.Address = row.Cell(3).Value.ToString();
                                            if (TryValidateModel(factory, nameof(Factory)))
                                            {
                                                _context.Factory.Add(factory);
                                            }
                                            else
                                            {
                                                errorCount++;
                                                continue;
                                            }
                                        }

                                        int i = 4;
                                        while (true)
                                        {
                                            if (row.Cell(i).Value.ToString().Length != 0)
                                            {
                                                GarbageType type;

                                                var t = (from typ in _context.GarbageType
                                                         where typ.Name.Contains(row.Cell(i).Value.ToString())
                                                         select typ).ToList();
                                                if (t.Count > 0)
                                                {
                                                    type = t[0];
                                                }
                                                else
                                                {
                                                    type      = new GarbageType();
                                                    type.Name = row.Cell(i).Value.ToString();
                                                    if (!TryValidateModel(type, nameof(GarbageType)))
                                                    {
                                                        errorCount++;
                                                    }
                                                    _context.Add(type);
                                                }
                                                FactoryGarbageType ft = new FactoryGarbageType();
                                                ft.IdFactoryNavigation     = factory;
                                                ft.IdGarbageTypeNavigation = type;
                                                _context.FactoryGarbageType.Add(ft);
                                                ++i;
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        ++errorCount;
                                    }
                                }
                            }
                        }
                    }
                }

                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index), new { errorCount = errorCount }));
        }