public async Task <IActionResult> Edit(int id, int?specialtiesId, [Bind("Id,Name,SpecialtiesId,Price")] EducationalProg educationalProg) { if (id != educationalProg.Id) { return(NotFound()); } ViewBag.SpecialtiesId = specialtiesId; if (ModelState.IsValid) { try { _context.Update(educationalProg); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EducationalProgExists(educationalProg.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index", "EducationalProgs", new { id = specialtiesId })); } ViewData["SpecialtiesId"] = new SelectList(_context.Specialties, "Id", "Name", educationalProg.SpecialtiesId); //return View(educationalProg); return(RedirectToAction("Index", "EducationalProgs", new { id = specialtiesId })); }
public async Task <IActionResult> Create(int specialtiesId, [Bind("Id,Name,SpecialtiesId,Price")] EducationalProg educationalProg) { educationalProg.SpecialtiesId = specialtiesId; if (ModelState.IsValid) { _context.Add(educationalProg); await _context.SaveChangesAsync(); //return RedirectToAction(nameof(Index)); return(RedirectToAction("Index", "EducationalProgs", new { id = specialtiesId, name = _context.Specialties.Where(c => c.Id == specialtiesId).FirstOrDefault().Name })); } //ViewData["SpecialtiesId"] = new SelectList(_context.Specialties, "Id", "Name", educationalProg.SpecialtiesId); //return View(educationalProg); return(RedirectToAction("Index", "EducationalProgs", new { id = specialtiesId, name = _context.Specialties.Where(c => c.Id == specialtiesId).FirstOrDefault().Name })); }
public async Task <IActionResult> Import(IFormFile fileExcel) { //string ErrorMes, err; 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) { //worksheet.Name - назва категорії. Пробуємо знайти в БД, якщо відсутня, то створюємо нову Specialties newcat; if (worksheet.Name.Count() > 31) { worksheet.Name = worksheet.Name.Substring(0, 30); } var c = (from cat in _context.Specialties where cat.Name == worksheet.Name + " " + worksheet.Cell(2, 8).Value.ToString() select cat).ToList(); if (c.Count > 0) { newcat = c[0]; } else { newcat = new Specialties(); newcat.Name = worksheet.Name.Substring(0, 3) + " " + worksheet.Cell(2, 8).Value.ToString(); newcat.Info = "from EXCEL"; //додати в контекст _context.Specialties.Add(newcat); } //перегляд усіх рядків foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { Countries country = new Countries(); Universities university = new Universities(); Faculties faculties = new Faculties(); EducationalProg educationalProg = new EducationalProg(); var edu = (from ed in _context.EducationalProg where ed.Name == row.Cell(1).Value.ToString() select ed).ToList(); if (edu.Count > 0) { educationalProg = edu[0]; } else { educationalProg = new EducationalProg(); educationalProg.Name = row.Cell(1).Value.ToString(); //educationalProg.Info = row.Cell(6).Value.ToString(); educationalProg.Specialties = newcat; _context.EducationalProg.Add(educationalProg); } //у разі наявності факультету знайти його, у разі відсутності - додати for (int i = 2; i <= 5; i = i + 3) { if (row.Cell(i).Value.ToString().Length > 0) { try { if (row.Cell(i + 2).Value.ToString().Length > 0) { var co = (from coun in _context.Countries where coun.Name.Contains(row.Cell(i + 2).Value.ToString()) select coun).ToList(); if (co.Count > 0) { country = co[0]; } else { country = new Countries(); country.Name = row.Cell(i + 2).Value.ToString(); //додати в контекст _context.Countries.Add(country); } } else { throw new Exception(" Країну не вірно вказано у спеціальності " + worksheet.Name + "у рядку " + row.RowNumber()); } } catch (Exception e) { ViewBag.ErrorMes = e.Message; return(RedirectToAction("Index", "Specialties", new { error = e.Message })); } try { if (row.Cell(i + 1).Value.ToString().Length > 0) { var u = (from univ in _context.Universities where univ.Country.Name.Contains(country.Name) && univ.Name == row.Cell(i + 1).Value.ToString() select univ).ToList(); if (u.Count > 0) { university = u[0]; } else { university = new Universities(); university.Name = row.Cell(i + 1).Value.ToString(); university.Country = country; //додати в контекст _context.Universities.Add(university); } } else { throw new Exception("Університет не вказано у спеціальності " + worksheet.Name + " у рядку " + row.RowNumber().ToString()); } } catch (Exception e) { ViewBag.ErrorMes = e.Message; return(RedirectToAction("Index", "Specialties", new { error = e.Message })); } var a = (from fac in _context.Faculties where fac.University.Name == university.Name && fac.University.Country.Name.Contains(country.Name) && fac.Name == row.Cell(i).Value.ToString() select fac).ToList(); if (a.Count > 0) { faculties = a[0]; } else { faculties = new Faculties(); faculties.Name = row.Cell(i).Value.ToString(); faculties.University = university; //додати в контекст _context.Faculties.Add(faculties); } FacultyEducationalProg fe; fe = new FacultyEducationalProg(); fe.EducationalProg = educationalProg; fe.Faculty = faculties; _context.FacultyEducationalProg.Add(fe); } } } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }