我在lambda表达式中遇到错误时遇到了一些麻烦。如果您有解决方案,请分享。
处理请求时发生未处理的异常。 NullReferenceException:对象引用未设置为对象的实例。 lambda_method(关闭)
我试图用断点调试它,但是徒劳。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using InformationSystem.Data;
using InformationSystem.Models;
using InformationSystem.Models.ViewModels;
using Microsoft.AspNetCore.Authorization;
using InformationSystem.Policies;
using Microsoft.AspNetCore.Identity;
namespace InformationSystem.Controllers
{
public class AttendancesController : Controller
{
private readonly ApplicationDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
public AttendancesController(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
_context = context;
}
// GET: Attendances
public async Task<IActionResult> Index()
{
var applicationDbContext = _context.Attendances.Include(a => a.CourseAllocation).Include(c => c.Lacture).Include(c => c.Student).Include(c => c.CourseAllocation.Course).Include(c => c.CourseAllocation.BatchProgramSemesterSection);
return View(await applicationDbContext.ToListAsync());
}
// GET: Attendances/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var attendance = await _context.Attendances
.Include(a => a.CourseAllocation)
.SingleOrDefaultAsync(m => m.Id == id);
if (attendance == null)
{
return NotFound();
}
return View(attendance);
}
public async Task<IActionResult> AttendenceSheet()
{
var UserInfo = await this._userManager.GetUserAsync(this.User);
var TeacherAllocatedData = await _context.UserTeacherAllocation.FirstOrDefaultAsync(m => m.ApplicationUserId == UserInfo.Id);
var teacherObj = await _context.Teacher.FirstOrDefaultAsync(i => i.Id == TeacherAllocatedData.TeacherId); //This line breaks
var CourseAllocationList = await _context.CourseAllocations.Where(c => c.Teacher.Id == teacherObj.Id).ToListAsync();
ViewData["CourseAllocationId"] = new SelectList(CourseAllocationList, "Id", "Name");
// ViewData["Teacher"] = new SelectList(_context.Teacher, "Id", "TeacherName");
ViewData["LactureId"] = new SelectList(_context.Lactures, "Id", "Name");
ViewData["StudentCourseAllocationId"] = new SelectList(_context.CourseAllocations, "Id", "Id");
return View(teacherObj);
}
// GET: Attendances/Create
[Authorize(AuthPolicies.AttendedenceCreate)]
public async Task<IActionResult> Create()
{
var UserInfo = await this._userManager.GetUserAsync(this.User);
var TeacherAllocatedData = await _context.UserTeacherAllocation.FirstOrDefaultAsync(m=> m.ApplicationUserId == UserInfo.Id);
var teacherObj = await _context.Teacher.FirstOrDefaultAsync(i => i.Id == TeacherAllocatedData.TeacherId);
var CourseAllocationList = await _context.CourseAllocations.Where(c => c.Teacher.Id == teacherObj.Id).ToListAsync();
ViewData["CourseAllocationId"] = new SelectList(CourseAllocationList, "Id", "Name");
// ViewData["Teacher"] = new SelectList(_context.Teacher, "Id", "TeacherName");
ViewData["LactureId"] = new SelectList(_context.Lactures.OrderBy(m => m.Number), "Id", "Name");
ViewData["StudentCourseAllocationId"] = new SelectList(_context.CourseAllocations, "Id", "Id");
return View(teacherObj);
}
// POST: Attendances/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(AuthPolicies.AttendedenceCreate)]
public async Task<IActionResult> Create([Bind("TeacherId,batchProgramSemesterSectionId,students,CourseAllocation,AttendenceDate,Course,checkForAttendences")]AttendenceCreateViewModel AttendenceViewModel, Attendance attendance)
{
if (ModelState.IsValid)
{
_context.Add(attendance);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CourseAllocationId"] = new SelectList(_context.StudentCourseAllocations, "Id", "Id", attendance.CourseAllocationId);
return View(attendance);
}
public JsonResult SelectSection(int Id)
{
List<BatchProgramSemesterSection> sections = new List<BatchProgramSemesterSection>();
var listSel = _context.CourseAllocations.Include(c => c.Teacher).Include(c => c.BatchProgramSemesterSection).Where(c => c.Teacher.Id == Id).ToList();
foreach (var item in listSel)
{
sections.Add(item.BatchProgramSemesterSection);
}
sections.Insert(0, new BatchProgramSemesterSection { Id = 0, Name = "Select Section" });
return Json(new SelectList(sections, "Id", "Name"));
}
public JsonResult SelectCourse(int Id)
{
List<Course> courses = new List<Course>();
var listSel = _context.CourseAllocations.Include(c => c.Course).Include(c => c.BatchProgramSemesterSection).Where(c => c.BatchProgramSemesterSectionId == Id).ToList();
foreach (var item in listSel)
{
courses.Add(item.Course);
}
courses.Insert(0, new Course { Id = 0, Name = "Select Course" });
return Json(new SelectList(courses, "Id", "Name"));
}
public IActionResult AttendenceByBatch()
{
ViewData["Batches"] = new SelectList(_context.Batches, "Id", "Name");
return View();
}
public IActionResult AttendenceByBatchPV(int Id, int Ratio)
{
var Lactures = _context.Lactures.ToList();
ViewBag.Semesters = _context.Semester.ToList();
var ViewModelList = new List<AttendenceByBatchViewModel>();
var programList = _context.BatchProgram.Include(c => c.Batch).Include(c => c.DepartmentProgram).Where(c => c.BatchId == Id).ToList();
var SemesterList = _context.Semester.ToList();
foreach (var item in programList)
{
var ViewModelObject = new AttendenceByBatchViewModel();
ViewModelObject.ProgramName = item.DepartmentProgram.Name;
int ShortStudentCount = 0;
foreach (var item2 in SemesterList)
{
try
{
var StudentsForSheet = _context.Students.Include(c => c.BatchProgramSectionSemester).Where(c => c.BatchProgramSectionSemester.BatchProgramSemesterId == item.Id && c.BatchProgramSectionSemester.BatchProgramSemester.BatchSemester.SemesterId == item2.Id).ToList();
foreach (var item3 in StudentsForSheet)
{
int Count = 0;
var AttendenceList = _context.Attendances.Include(s => s.Student).Include(c => c.CourseAllocation).Where(m => m.StudentId == item3.Id).ToList();
foreach (var item4 in AttendenceList)
{
if (item4.AttendanceDay)
{
Count++;
}
}
double CountPercent = Count;
double AttendecePercentage = Math.Round((CountPercent / AttendenceList.Count() * 100), 2);
if (AttendecePercentage < Ratio)
{
ShortStudentCount++;
}
}
ViewModelObject.StudentCount.Add(ShortStudentCount);
}
catch (NullReferenceException)
{
int i = 0;
ViewModelObject.StudentCount.Add(i);
}
}
ViewModelList.Add(ViewModelObject);
}
return PartialView("Partial/AttendenceByBatchPV", ViewModelList);
}
public IActionResult AttendenceBySemester()
{
ViewData["BPSS"] = new SelectList(_context.BatchProgramSemesters, "Id", "Name");
return View();
}
public IActionResult AttendenceBySemesterPV(int Id, int Ratio)
{
var ViewModelList = new List<AttendenceListBySemester>();
var LactureCount = _context.Lactures.ToList();
var StudentsForShet = _context.StudentCourseAllocations.Include(c => c.Student).Include(c => c.CourseAllocation.Course).Include(c => c.CourseAllocation.BatchProgramSemesterSection.BatchProgramSemester).Where(c => c.CourseAllocation.BatchProgramSemesterSection.BatchProgramSemesterId == Id).ToList();
foreach (var item in StudentsForShet)
{
int Count = 0;
List<Boolean> PAList = new List<Boolean>();
var viewModelObj = new AttendenceListBySemester();
viewModelObj.Student = item.Student.Name;
viewModelObj.RollNumber = item.Student.RollNumber;
var StudentAttendence = _context.Attendances.Include(i => i.Student).Include(i => i.CourseAllocation.Course).Include(i => i.CourseAllocation.BatchProgramSemesterSection).Where(c => c.Student.Id == item.Student.Id && c.CourseAllocation.BatchProgramSemesterSectionId == item.CourseAllocation.BatchProgramSemesterSectionId && c.CourseAllocation.CourseId == item.CourseAllocation.CourseId).ToList();
foreach (var item2 in StudentAttendence)
{
PAList.Add(item2.AttendanceDay);
if (item2.AttendanceDay)
{
Count++;
}
}
double CountPercent = Count;
double AttendecePercentage = Math.Round((CountPercent / LactureCount.Count() * 100), 2);
if (AttendecePercentage > Ratio)
{
viewModelObj.Status = "OK";
}
else
{
viewModelObj.Status = "Short";
}
viewModelObj.AttendenceCount = Count;
viewModelObj.Percentage = AttendecePercentage;
ViewModelList.Add(viewModelObj);
}
return PartialView("Partial/AttendenceBySemesterPV", ViewModelList);
}
public async Task<IActionResult> GetMonth(int Id, int TeacherId, int CourseId)
{
var CourseAllocationId = await _context.CourseAllocations.Where(m => m.TeacherId == TeacherId && m.CourseId == CourseId && m.BatchProgramSemesterSectionId == Id).SingleOrDefaultAsync();
var AttendenceDates = await _context.Attendances.Where(m => m.CourseAllocationId == CourseAllocationId.Id).ToListAsync();
string DateCheck = "";
var DateList = new List<string>();
foreach (var item in AttendenceDates)
{
if (DateCheck != item.Attendencedate.ToString("yy"))
{
DateCheck = item.Attendencedate.ToString("yy");
DateList.Add(DateCheck);
}
}
return Json(new SelectList(DateList));
}
public async Task<IActionResult> StudentAttendenceList(int bSSId, int TeacherId, int LactureId)
{
ViewData["LactureList"] = await _context.Lactures.OrderBy(m => m.Number).ToListAsync();
var ViewModelList = new List<AttendenceSummryViewModel>();
var LactureList = await _context.Lactures.OrderBy(m => m.Number).ToListAsync();
var StudentsForShet = await _context.StudentCourseAllocations.Include(c => c.Student).Include(c => c.CourseAllocation.Course).Include(c => c.CourseAllocation.BatchProgramSemesterSection).Where(c => c.CourseAllocationID == bSSId).ToListAsync();
foreach (var item in StudentsForShet)
{
int Count = 0;
var viewModelObj = new AttendenceSummryViewModel();
viewModelObj.Student = item.Student.Name;
viewModelObj.RollNO = item.Student.RollNumber;
foreach (var item3 in LactureList)
{
var StudentAttendence = await _context.Attendances?.Include(i => i.Student).Include(i => i.CourseAllocation.Course).Include(i => i.CourseAllocation.BatchProgramSemesterSection).Where(c => c.Student.Id == item.Student.Id && c.CourseAllocation.BatchProgramSemesterSectionId == item.CourseAllocation.BatchProgramSemesterSectionId && c.CourseAllocation.CourseId == item.CourseAllocation.CourseId && c.LactureId == item3.Id).FirstOrDefaultAsync();
if (StudentAttendence != null)
{
if (StudentAttendence.AttendanceDay)
{
Count++;
viewModelObj.EmptySpaces.Add("P");
}
else
{
viewModelObj.EmptySpaces.Add("A");
}
}
else
{
viewModelObj.EmptySpaces.Add("");
}
}
double CountPercent = Count;
double AttendecePercentage = Math.Round((CountPercent / LactureList.Count() * 100), 2);
viewModelObj.Pecentage = AttendecePercentage;
viewModelObj.AttendenceCount = Count;
ViewModelList.Add(viewModelObj);
}
return PartialView("Partial/AttendenceSheetPV", ViewModelList);
}
public async Task<IActionResult> StudentList(int BSSPiD, int TeacherId, int LactureId, DateTime attendenceDate)
{
var courseAllocationObject = await _context.CourseAllocations.Where(m => m.Id == BSSPiD).FirstOrDefaultAsync();
var AttendenceCheck = await _context.Attendances.Where(m => m.AttendenceCheck.Contains("L" + LactureId + "C" + courseAllocationObject.Id)).FirstOrDefaultAsync();
if (AttendenceCheck != null)
{
return PartialView("Partial/AttendenceError");
}
var viewModel = new AttendenceCreateViewModel();
List<Student> StudentList = new List<Student>();
var StudentCourseAllocationsList = await _context.StudentCourseAllocations.Include(c => c.Student).Include(c => c.CourseAllocation.Course).Include(c => c.CourseAllocation.BatchProgramSemesterSection).Where(c => c.CourseAllocationID == BSSPiD).ToListAsync();
foreach (var item in StudentCourseAllocationsList)
{
StudentList.Add(item.Student);
}
viewModel.Students = StudentList;
viewModel.CousrAllocationId = courseAllocationObject.Id;
viewModel.Date = attendenceDate;
viewModel.LactureId = LactureId;
return PartialView("Partial/StudentPV", viewModel);
}
[HttpPost]
public async Task<IActionResult> AttendencePost([Bind("LactureId,CousrAllocationId,Date,Students,checkForAttendences")]AttendenceCreateViewModel viewModel)
{
if (ModelState.IsValid)
{
for (int i = 0; i < viewModel.Students.Count(); i++)
{
Attendance attendance = new Attendance
{
AttendanceDay = viewModel.checkForAttendences[i],
Attendencedate = viewModel.Date,
CourseAllocationId = viewModel.CousrAllocationId,
StudentId = viewModel.Students[i].Id,
LactureId = viewModel.LactureId,
AttendenceCheck = "L" + viewModel.LactureId + "C" + viewModel.CousrAllocationId + "S" + viewModel.Students[i]
};
await _context.AddAsync(attendance);
}
await _context.SaveChangesAsync();
}
return RedirectToAction("Index");
}
// GET: Attendances/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var attendance = await _context.Attendances.SingleOrDefaultAsync(m => m.Id == id);
if (attendance == null)
{
return NotFound();
}
ViewData["StudentCourseAllocationId"] = new SelectList(_context.StudentCourseAllocations, "Id", "Id", attendance.CourseAllocation);
return View(attendance);
}
// POST: Attendances/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(AuthPolicies.AttendedenceEdit)]
public async Task<IActionResult> Edit(int id, [Bind("StudentCourseAllocationId,AttendanceDay,Id")] Attendance attendance)
{
if (id != attendance.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(attendance);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!AttendanceExists(attendance.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["CourseAllocationId"] = new SelectList(_context.StudentCourseAllocations, "Id", "Id", attendance.CourseAllocation);
return View(attendance);
}
// GET: Attendances/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var attendance = await _context.Attendances
.Include(a => a.CourseAllocation)
.SingleOrDefaultAsync(m => m.Id == id);
if (attendance == null)
{
return NotFound();
}
return View(attendance);
}
// POST: Attendances/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var attendance = await _context.Attendances.SingleOrDefaultAsync(m => m.Id == id);
_context.Attendances.Remove(attendance);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool AttendanceExists(int id)
{
return _context.Attendances.Any(e => e.Id == id);
}
}
}
我希望它能够运行,并且IIS显示错误在第70行