public async Task <IActionResult> Edit(int id, int facultyId, [Bind("Id,EducationalProgId,FacultyId")] FacultyEducationalProg facultyEducationalProg) { if (id != facultyEducationalProg.Id) { return(NotFound()); } ViewBag.FacultyId = facultyId; ViewBag.EducationalProgId = facultyEducationalProg.EducationalProgId; if (ModelState.IsValid) { try { _context.Update(facultyEducationalProg); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FacultyEducationalProgExists(facultyEducationalProg.Id)) { return(NotFound()); } else { throw; } } //return RedirectToAction(nameof(Index)); return(RedirectToAction("Index", "FacultyEducationalProgs", new { id = facultyId })); } ViewData["EducationalProgId"] = new SelectList(_context.EducationalProg, "Id", "Name", facultyEducationalProg.EducationalProgId); ViewData["FacultyId"] = new SelectList(_context.Faculties, "Id", "Name", facultyEducationalProg.FacultyId); //return View(facultyEducationalProg); return(RedirectToAction("Index", "FacultyEducationalProgs", new { id = facultyId })); }
public async Task <IActionResult> Create(int educationalProgId, int facultyId, [Bind("Id,EducationalProgId,FacultyId")] FacultyEducationalProg facultyEducationalProg) { // facultyEducationalProg.EducationalProgId = educationalProgId; // facultyEducationalProg.FacultyId = facultyId; // if (ModelState.IsValid) // { _context.Add(facultyEducationalProg); await _context.SaveChangesAsync(); //return RedirectToAction(nameof(Index)); // return RedirectToAction("Index", "FacultyEducationalProgs", new { id = educationalProgId, name = _context.EducationalProg.Where(h => h.Id == educationalProgId).FirstOrDefault().Name }); // } //ViewData["EducationalProgId"] = new SelectList(_context.EducationalProg, "Id", "Name", facultyEducationalProg.EducationalProgId); //ViewData["FacultyId"] = new SelectList(_context.Faculties, "Id", "Name", facultyEducationalProg.FacultyId); //return View(facultyEducationalProg); //return RedirectToAction("Index", "FacultyEducationalProgs", new { id = educationalProgId, name = _context.EducationalProg.Where(h => h.Id == educationalProgId).FirstOrDefault().Name }); return(RedirectToAction("Index", "FacultyEducationalProgs", new { id = facultyId })); }
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))); }