public async Task <IActionResult> Create(int countryId, [Bind("Id,Name,CreationDate")] Organisation organisation) { organisation.CountryId = countryId; if (ModelState.IsValid) { var c = (from org in _context.Organisations where org.Name.Contains(organisation.Name) select org).ToList(); if (c.Count > 0) { return(RedirectToAction("Index", "Organisations", new { id = countryId, name = _context.Countries.Where(c => c.Id == countryId).FirstOrDefault().Name })); } if (!Valid.Datecheck(organisation.CreationDate)) { return(RedirectToAction("Index", "Organisations", new { id = countryId, name = _context.Countries.Where(c => c.Id == countryId).FirstOrDefault().Name })); } _context.Add(organisation); await _context.SaveChangesAsync(); // return RedirectToAction(nameof(Index)); return(RedirectToAction("Index", "Organisations", new { id = countryId, name = _context.Countries.Where(c => c.Id == countryId).FirstOrDefault().Name })); } // ViewData["CountryId"] = new SelectList(_context.Countries, "Id", "Name", organisation.CountryId); // return View(organisation); return(RedirectToAction("Index", "Organisations", new { id = countryId, name = _context.Countries.Where(c => c.Id == countryId).FirstOrDefault().Name })); }
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); using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled)) { foreach (IXLWorksheet worksheet in workBook.Worksheets) { if (!Valid.IsAllLetters(worksheet.Name)) { continue; } Country newcoun; var c = (from coun in _context.Countries where coun.Name.Contains(worksheet.Name) select coun).ToList(); if (c.Count > 0) { newcoun = c[0]; } else { newcoun = new Country(); newcoun.Name = worksheet.Name; _context.Countries.Add(newcoun); } foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { try { Organisation organisation = new Organisation(); organisation.Name = row.Cell(1).Value.ToString(); organisation.CreationDate = DateTime.Parse(row.Cell(2).Value.ToString()); if (!Valid.Datecheck(organisation.CreationDate)) { continue; } //int Day = Int.Parse(row.Cell(2).Value.ToString()); //int Mounth = Int.Parse(row.Cell(3).Value.ToString()); //int Year = Int.Parse(row.Cell(4).Value.ToString()); //organisation.CreationDate = new DateTime(Year, Mounth, Day); organisation.Country = newcoun; var dcp = (from org in _context.Organisations where org.Name.Contains(organisation.Name) select org).ToList(); if (dcp.Count > 0) { continue; } _context.Organisations.Add(organisation); } catch (Exception e) { } } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }