0% found this document useful (0 votes)
2 views4 pages

Enrollmentcontroller

The document outlines the implementation of a StudentDetailsController in an ASP.NET Core MVC application for managing student details. It includes methods for displaying a list of student details, creating new entries, editing existing ones, and deleting records, with support for pagination and filtering. The controller interacts with a database context to retrieve and manipulate student data, utilizing Entity Framework Core for data operations.

Uploaded by

dayen55024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Enrollmentcontroller

The document outlines the implementation of a StudentDetailsController in an ASP.NET Core MVC application for managing student details. It includes methods for displaying a list of student details, creating new entries, editing existing ones, and deleting records, with support for pagination and filtering. The controller interacts with a database context to retrieve and manipulate student data, utilizing Entity Framework Core for data operations.

Uploaded by

dayen55024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using Microsoft.AspNetCore.

Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TicketCore.Data;
using TicketCore.Models.StudentDetails;
using TicketCore.Models.Student;
using TicketCore.Models.Seria;
using TicketCore.Models.Specializare;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace TicketCore.Web.Areas.Administration.Controllers
{
[Area("Administration")]
public class StudentDetailsController : Controller
{
private readonly VueTicketDbContext _vueTicketDbContext;

public StudentDetailsController(VueTicketDbContext vueTicketDbContext)


{
_vueTicketDbContext = vueTicketDbContext;
}

public async Task<IActionResult> Index(string sortOrder, string


currentFilter, string searchString, int? pageNumber)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ?
"name_desc" : "";

if (searchString != null)
{
pageNumber = 1;
}
else
{
searchString = currentFilter;
}

ViewData["CurrentFilter"] = searchString;

var result = from details in _vueTicketDbContext.StudentDetails

join student in _vueTicketDbContext.Student on


details.StudentId equals student.StudentId
join specializare in _vueTicketDbContext.Specializare on
details.SpecID equals specializare.ID
join seria in _vueTicketDbContext.Seria on details.SeriaId
equals seria.SeriaId
select new StudentDetails
{
StudID = details.StudID,
StudentId = student.StudentId,
FullName = student == null ? "" : student.FullName,
NrMatricol = details.NrMatricol,
SpecID = specializare.ID,
Specializare_ani = specializare == null ? "" :
specializare.Specializare_ani,
SeriaId = seria.SeriaId,
Seria_ani = seria == null ? "" : seria.Seria_ani
};

int pageSize = 5; // Set your desired page size here


ViewBag.PageSize = pageSize; // Pass it to the view using ViewBag
return View(await
PaginatedList<StudentDetails>.CreateAsync(result.AsNoTracking(), pageNumber ?? 1,
pageSize));
}

public IActionResult Create(StudentDetails obj)


{

List<Student> students = _vueTicketDbContext.Student.ToList();


List<Seria> serii = _vueTicketDbContext.Seria.ToList();
List<Specializare> specializari =
_vueTicketDbContext.Specializare.ToList();

// Filtrați lista de cursuri pentru a face dnumele unic


var uniqueStudents = students.GroupBy(student => student.FullName)
.Select(group => group.First())
.ToList();

// Puteți utiliza ViewBag sau un model de vedere pentru a transmite


aceste liste la vedere
ViewBag.StudentList = new SelectList(uniqueStudents, "StudentId",
"FullName");
ViewBag.SeriaList = new SelectList(serii, "SeriaId", "Seria_ani");
ViewBag.SpecializareList = new SelectList(specializari, "ID",
"Specializare_ani");

// Restul codului pentru afișarea formularului CreateEnrollment


return View(obj);
}

[HttpPost]
public async Task<IActionResult> AddDetails(StudentDetails obj)
{
try
{
if (ModelState.IsValid)
{
if (obj.StudID == 0)
{
_vueTicketDbContext.StudentDetails.Add(obj);
await _vueTicketDbContext.SaveChangesAsync();
}
else
{
_vueTicketDbContext.Entry(obj).State =
EntityState.Modified;
await _vueTicketDbContext.SaveChangesAsync();
}

return RedirectToAction("Index");
}

return View(obj);
}
catch (Exception ex)
{

return RedirectToAction("Index");
}

[HttpPost]

public IActionResult Edit(int StudID)


{
try
{
var std = _vueTicketDbContext.StudentDetails.FirstOrDefault(e =>
e.StudID == StudID);
if (std == null)
{
return NotFound();
}

List<Student> students = _vueTicketDbContext.Student.ToList();


List<Specializare> specializari =
_vueTicketDbContext.Specializare.ToList();
List<Seria> serii = _vueTicketDbContext.Seria.ToList();

ViewBag.StudentList = new SelectList(students, "StudentId",


"FullName");
ViewBag.SpecializareList = new SelectList(specializari, "ID",
"Specializare_ani");
ViewBag.SeriaList = new SelectList(serii, "SeriaId", "Seria_ani");

return View(StudID);
}
catch (Exception ex)
{
return RedirectToAction("Index");
}
}

public async Task<IActionResult> Delete(int StudID)


{
try
{
var std = await
_vueTicketDbContext.StudentDetails.FindAsync(StudID);
if (std != null)
{
_vueTicketDbContext.StudentDetails.Remove(std);
await _vueTicketDbContext.SaveChangesAsync();
}

return RedirectToAction("Index");
}
catch (Exception ex)
{

return RedirectToAction("Index");
}
}

}
}

You might also like