public async Task <IActionResult> Edit(int id, int facultyID, [Bind("Id,CathedraName")] Cathedras cathedras) { /* var elements = _context.Cathedras.ToArray(); * foreach (var c in elements) * { * if (cathedras.CathedraName == c.CathedraName) * { * ModelState.AddModelError("CathedraName", "Coppy Error"); * return View(cathedras); * } * } */ cathedras.FacultyId = facultyID; if (id != cathedras.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(cathedras); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CathedrasExists(cathedras.Id)) { return(NotFound()); } else { throw; } } // return RedirectToAction(nameof(Index)); // return RedirectToAction("Index", "Cathedras", new { id = facultyID, name = _context.Faculties.Where(c => c.Id == facultyID).FirstOrDefault() }); return(RedirectToAction("Index", "Cathedras", new { id = facultyID, name = _context.Faculties.Where(c => c.Id == facultyID).FirstOrDefault().FacultyName })); } // ViewData["FacultyId"] = new SelectList(_context.Faculties, "Id", "Id", cathedras.FacultyId); //return View(cathedras); return(RedirectToAction("Index", "Cathedras", new { id = facultyID, name = _context.Faculties.Where(c => c.Id == facultyID).FirstOrDefault().FacultyName })); }
protected void LoadEntities() { Addresses.Load(); Cathedras.Load(); Citizenships.Load(); CommandPermissions.Load(); Commands.Load(); Countries.Load(); Directions.Load(); DisciplineCycles.Load(); Disciplines.Load(); Districts.Load(); EducationCompetences.Load(); EducationForms.Load(); EducationLevels.Load(); EducationPlanCompoments.Load(); EducationPlanGraphics.Load(); EducationPlanItems.Load(); EducationPlans.Load(); EducationProgramTypes.Load(); EducationStandartTypes.Load(); Faculties.Load(); FinanceSources.Load(); Genders.Load(); Groups.Load(); IdentityDocuments.Load(); IdentityOrganizations.Load(); IdentityTypes.Load(); InterfaceElements.Load(); InterfacePermissions.Load(); Localities.Load(); Persons.Load(); Regions.Load(); Roles.Load(); Streets.Load(); StudentStates.Load(); Students.Load(); Teachers.Load(); Towns.Load(); Users.Load(); }
public async Task <IActionResult> Create(int facultyID, [Bind("Id,CathedraName")] Cathedras cathedras) { /* var elements = _context.Cathedras.ToArray(); * foreach (var c in elements) * { * if (cathedras.CathedraName == c.CathedraName) * { * ModelState.AddModelError("CathedraName", "Coppy Error"); * return View(cathedras); * } * } */ cathedras.FacultyId = facultyID; if (ModelState.IsValid) { _context.Add(cathedras); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Cathedras", new { id = facultyID, name = _context.Faculties.Where(c => c.Id == facultyID).FirstOrDefault().FacultyName })); } return(RedirectToAction("Index", "Cathedras", new { id = facultyID, name = _context.Faculties.Where(c => c.Id == facultyID).FirstOrDefault() })); }
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) { //worksheet.Name - назва категорії. Пробуємо знайти в БД, якщо відсутня, то створюємо нову Faculties newfac; var c = (from fac in _context.Faculties where fac.FacultyName.Contains(worksheet.Name) select fac).ToList(); if (c.Count > 0) { newfac = c[0]; } else { newfac = new Faculties(); newfac.FacultyName = worksheet.Name; // newgrp.Info = "from EXCEL"; //додати в контекст _context.Faculties.Add(newfac); } //перегляд усіх рядків foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { try { Cathedras newcath = null; bool flagg = false; string cathName = row.Cell(1).Value.ToString(); var v = _context.Cathedras.ToArray(); foreach (Cathedras cath in v) { string cath2Name = cath.CathedraName.ToString(); if (cath2Name.Contains(cathName)) { flagg = true; newcath = cath; break; } } /* string cathName = row.Cell(1).Value.ToString(); * var v = (from cath in _context.Cathedras //.Where(k => k.FacultyId == newfac.Id) * where cath.CathedraName.Contains(cathName) * select cath).ToList(); * if (v.Count>0) * { * newcath = v[0]; * } */ if (!flagg) { newcath = new Cathedras(); newcath.CathedraName = row.Cell(1).Value.ToString(); newcath.FacultyId = newfac.Id; newcath.Faculty = newfac; // newgrp.Info = "from EXCEL"; //додати в контекст _context.Cathedras.Add(newcath); } Group newgrp; string grpName = row.Cell(2).Value.ToString(); var b = (from grp in _context.Group where grp.GroupName.Contains(grpName) select grp).ToList(); if (b.Count > 0) { newgrp = b[0]; } else { newgrp = new Group(); newgrp.GroupName = row.Cell(2).Value.ToString(); newgrp.TypeOfStudying = Convert.ToInt32(row.Cell(3).Value.ToString()); newgrp.CathedraId = newcath.Id; newgrp.Cathedra = newcath; // newgrp.Info = "from EXCEL"; //додати в контекст _context.Group.Add(newgrp); } Aspirant aspirant = new Aspirant(); aspirant.Name = row.Cell(4).Value.ToString(); aspirant.Surname = row.Cell(5).Value.ToString(); aspirant.BirthDay = (DateTime)row.Cell(6).Value; // book.Info = row.Cell(6).Value.ToString(); aspirant.Group = newgrp; aspirant.GroupId = newgrp.Id; bool flag = true; var aspirants = _context.Aspirant.Where(n => n.GroupId == newgrp.Id).ToArray(); foreach (var asp in aspirants) { if ((asp.Name == aspirant.Name) && (asp.Surname == aspirant.Surname) && (asp.BirthDay == aspirant.BirthDay)) { flag = false; break; } } if (flag) { _context.Aspirant.Add(aspirant); await _context.SaveChangesAsync(); } } catch (Exception e) { //logging самостійно :) } } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }