public async Task <IActionResult> Create(int?id, [Bind("ProblemId,GradeId")] ProblemGrade problemGrade) { if (id == null) { return(NotFound()); } int ptId = 0; if (_context.ProblemGrade.Count() != 0) { ptId = _context.ProblemGrade.Max(p => p.Id) + 1; } problemGrade.Id = ptId; problemGrade.ProblemId = id; problemGrade.Problem = _context.Problem.Where(p => p.Id == id).FirstOrDefault(); problemGrade.Grade = _context.Grade.Where(t => t.Id == problemGrade.GradeId).FirstOrDefault(); if (ModelState.IsValid) { _context.ProblemGrade.Add(problemGrade); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "ProblemGrades", new { id = problemGrade.ProblemId })); } //ViewData["ProblemId"] = new SelectList(_context.Problem, "Id", "Id", problemTheme.ProblemId); //ViewData["ThemeId"] = new SelectList(_context.Theme, "Id", "ThemeName", problemTheme.ThemeId); return(RedirectToAction("Index", "ProblemGrades", new { id = problemGrade.ProblemId })); }
public async Task <IActionResult> Edit(int?id, [Bind("Id,ProblemId,GradeId")] ProblemGrade problemGrade) { if (id != problemGrade.ProblemId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(problemGrade); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProblemGradeExists(problemGrade.ProblemId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["GradeId"] = new SelectList(_context.Grade, "Id", "GradeName", problemGrade.GradeId); ViewData["ProblemId"] = new SelectList(_context.Problem, "Id", "Id", problemGrade.ProblemId); return(View(problemGrade)); }
public async Task <IActionResult> DeleteConfirmed(int?id) { ProblemGrade p = _context.ProblemGrade.Where(p => p.Id == id).FirstOrDefault(); int? pId = p.ProblemId; int? gId = p.GradeId; var problemGrade = await _context.ProblemGrade.FindAsync(pId, gId); _context.ProblemGrade.Remove(problemGrade); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Problems", new { id = pId })); }
public async Task <IActionResult> Import(IFormFile fileExcel) { if (ModelState.IsValid) { if (fileExcel != null) { using (var stream = new System.IO.FileStream(fileExcel.FileName, FileMode.Create)) { await fileExcel.CopyToAsync(stream); using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled)) { foreach (IXLWorksheet worksheet in workBook.Worksheets) { /*Theme newTheme; * var t = _context.Theme.Where(p => p.ThemeName == worksheet.Name).ToList(); * //var t = (from theme in _context.Theme * // where theme.ThemeName == worksheet.Name * // select theme).ToList(); * if(t.Count > 0) * { * newTheme = t[0]; * } * else * { * newTheme = new Theme(); * newTheme.ThemeName = worksheet.Name; * _context.Theme.Add(newTheme); * }*/ Theme newTheme = new Theme(); newTheme.ThemeName = worksheet.Name; foreach (var t in _context.Theme) { if (t.ThemeName == newTheme.ThemeName) { break; } else { _context.Theme.Add(newTheme); } } foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { try { Problem problem = new Problem(); problem.Statement = row.Cell(1).Value.ToString(); problem.Solution = row.Cell(2).Value.ToString(); problem.LevelId = (int)_context.Level.Where(p => p.Name == row.Cell(3).Value.ToString()).FirstOrDefault().Id; problem.SourceId = (int)_context.Source.Where(p => p.SourceName == row.Cell(4).Value.ToString()).FirstOrDefault().Id; ProblemGrade pg = new ProblemGrade(); pg.ProblemId = problem.Id; pg.GradeId = (int)_context.Grade.Where(g => g.GradeName == row.Cell(5).Value.ToString()).FirstOrDefault().Id; _context.ProblemGrade.Add(pg); ProblemTheme pt = new ProblemTheme(); pt.ProblemId = problem.Id; pt.ThemeId = (int)_context.Theme.Where(g => g.ThemeName == newTheme.ThemeName).FirstOrDefault().Id; _context.ProblemTheme.Add(pt); } catch (Exception e) { return(RedirectToAction(nameof(Index))); } } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }