Sınıflara öğrencileri atama işlemini ve CRUD işlemlerini gerçekleştirebiliriz. Aşağıda sınıf listeleri için gerekli adımlar ve dosya yapısı yer alıyor.
Controllers/Admin/SinifListeController.cs dosyasını oluşturarak CRUD işlemlerini gerçekleştirelim.
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SchoolManagement.Data;
using SchoolManagement.Models;
namespace SchoolManagement.Controllers
{
public class SinifListeController : Controller
{
private readonly ApplicationDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
public SinifListeController(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
// GET: SinifListe
public async Task<IActionResult> Index()
{
var siniflarVeMevcutlar = await _context.Siniflar
.Select(s => new SinifViewModel
{
Id = s.Id,
SinifAdi = s.SinifAdi,
Mevcudu = _context.SinifListeleri.Count(sl => sl.SinifId == s.Id)
})
.ToListAsync();
return View("~/Views/Admin/SinifListe/Index.cshtml", siniflarVeMevcutlar);
}
// GET: SinifListe/OgrenciAta/5
public async Task<IActionResult> OgrenciAta(int? id)
{
if (id == null)
{
return NotFound();
}
var sinif = await _context.Siniflar
.FirstOrDefaultAsync(s => s.Id == id);
if (sinif == null)
{
return NotFound();
}
// Sınıfa eklenmiş öğrenciler
var siniftakiOgrenciler = await _context.SinifListeleri
.Where(sl => sl.SinifId == id)
.Include(sl => sl.Ogrenci) // Ogrenci bilgilerini yükle
.ToListAsync();
// Öğrenci rolü ID'sini al
var ogrenciRoleId = await _context.Roles
.Where(r => r.Name == "Student")
.Select(r => r.Id)
.FirstOrDefaultAsync();
if (ogrenciRoleId == null)
{
TempData["Error"] = "Öğrenci rolü bulunamadı!";
return RedirectToAction(nameof(Index));
}
// Öğrenci rolüne sahip tüm kullanıcıları al
var ogrenciler = (from user in _context.Users
join userRole in _context.UserRoles on user.Id equals userRole.UserId
join role in _context.Roles on userRole.RoleId equals role.Id
where role.Name == "Student" && user.Durum == 1 // Öğrenci rolünü filtrele
select new ApplicationUser
{
Id = user.Id,
Numara = user.Numara,
Adi = user.Adi,
Soyadi = user.Soyadi
}).ToList();
// Sınıfta olmayan öğrencileri filtrele
var siniftaOlmayanOgrenciler = ogrenciler
.Where(o => !siniftakiOgrenciler.Any(so => so.OgrenciId == o.Id))
.ToList();
// Sınıfa eklenmiş öğrenciler ve sınıfta olmayan öğrenciler için ViewModel
var viewModel = new OgrenciAtaViewModel
{
SinifId = sinif.Id,
SinifAdi = sinif.SinifAdi,
SiniftakiOgrenciler = siniftakiOgrenciler,
SiniftaOlmayanOgrenciler = siniftaOlmayanOgrenciler
};
return View("~/Views/Admin/SinifListe/OgrenciAta.cshtml", viewModel);
}
// POST: SinifListe/OgrenciEkle
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OgrenciEkle(int sinifId, List<string> seciliOgrenciIdler)
{
if (seciliOgrenciIdler == null || !seciliOgrenciIdler.Any())
{
TempData["Error"] = "Lütfen en az bir öğrenci seçiniz.";
return RedirectToAction(nameof(OgrenciAta), new { id = sinifId });
}
foreach (var ogrenciId in seciliOgrenciIdler)
{
// Öğrenci zaten sınıfta var mı kontrol et
var mevcutKayit = await _context.SinifListeleri
.FirstOrDefaultAsync(sl => sl.SinifId == sinifId && sl.OgrenciId == ogrenciId);
if (mevcutKayit == null)
{
// Öğrenciyi sınıfa ekle
var yeniKayit = new SinifListe
{
SinifId = sinifId,
OgrenciId = ogrenciId,
NobetSayisi = 0 // Varsayılan değer
};
_context.Add(yeniKayit);
}
}
await _context.SaveChangesAsync();
TempData["Success"] = "Seçili öğrenciler sınıfa eklendi.";
return RedirectToAction(nameof(OgrenciAta), new { id = sinifId });
}
// POST: SinifListe/OgrenciCikar
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OgrenciCikar(int sinifId, List<string> seciliOgrenciIdler)
{
if (seciliOgrenciIdler == null || !seciliOgrenciIdler.Any())
{
TempData["Error"] = "Lütfen en az bir öğrenci seçiniz.";
return RedirectToAction(nameof(OgrenciAta), new { id = sinifId });
}
foreach (var ogrenciId in seciliOgrenciIdler)
{
// Öğrenciyi sınıftan çıkar
var kayit = await _context.SinifListeleri
.FirstOrDefaultAsync(sl => sl.SinifId == sinifId && sl.OgrenciId == ogrenciId);
if (kayit != null)
{
_context.SinifListeleri.Remove(kayit);
}
}
await _context.SaveChangesAsync();
TempData["Success"] = "Seçili öğrenciler sınıftan çıkarıldı.";
return RedirectToAction(nameof(OgrenciAta), new { id = sinifId });
}
}
}
Menüde, Sınıflar ve Öğrenciler için yeni bir buton ekleyelim. Bu buton, SinifListeController'ın Index action'ına yönlendirecek.
<li><a class="dropdown-item" asp-controller="SinifListe" asp-action="Index">Sınıflar ve Öğrenciler</a></li>
Sınıf listelerinin alınacağı model dosyasını oluşturalım.
namespace SchoolManagement.Models
{
// Sınıf listesi için görünüm modeli
public class SinifViewModel
{
public int Id { get; set; }
public string SinifAdi { get; set; }
public int Mevcudu { get; set; }
}
// Öğrenci atama sayfası için görünüm modeli
public class OgrenciAtaViewModel
{
public int SinifId { get; set; }
public string SinifAdi { get; set; }
public List<SinifListe> SiniftakiOgrenciler { get; set; } = new List<SinifListe>();
public List<ApplicationUser> SiniftaOlmayanOgrenciler { get; set; } = new List<ApplicationUser>();
}
}
Sınıf mevcutlarının listelendiği ana sayfayı Views/Admin/SinifListe/Index.cshtml dosyasına ekleyelim.
@model IEnumerable<SchoolManagement.Models.SinifViewModel>
@{
ViewData["Title"] = "Sınıf Listesi";
}
<h1>Sınıf Listesi</h1>
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th>Sınıf Adı</th>
<th>Mevcudu</th>
<th>İşlemler</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.SinifAdi</td>
<td>@item.Mevcudu</td>
<td>
<a asp-action="OgrenciAta" asp-route-id="@item.Id" class="btn btn-primary">
<i class="fa fa-user-plus"></i> Öğrenci Ata
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
Sınıflara öğrenci atama ve kaldırma işlemi için Views/Admin/SinifListe/OgrenciAta.cshtml dosyasını oluşturacağız.
@model SchoolManagement.Models.OgrenciAtaViewModel
@{
ViewData["Title"] = "Sınıf Öğrenci Yönetimi";
}
<h1>@Model.SinifAdi - Öğrenci Yönetimi</h1>
@if (TempData["Success"] != null)
{
<div class="alert alert-success">
@TempData["Success"]
</div>
}
@if (TempData["Error"] != null)
{
<div class="alert alert-danger">
@TempData["Error"]
</div>
}
<div class="row mt-4">
<div class="col-md-6">
<div class="card">
<div class="card-header bg-primary text-white">
<h5 class="card-title mb-0">Sınıfa Eklenebilecek Öğrenciler</h5>
</div>
<div class="card-body">
<form asp-action="OgrenciEkle" method="post">
<input type="hidden" name="sinifId" value="@Model.SinifId" />
@if (Model.SiniftaOlmayanOgrenciler.Any())
{
<div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
<table class="table table-striped table-bordered">
<thead class="table-light sticky-top">
<tr>
<th>Seç</th>
<th>Numara</th>
<th>Adı</th>
<th>Soyadı</th>
</tr>
</thead>
<tbody>
@foreach (var ogrenci in Model.SiniftaOlmayanOgrenciler)
{
<tr>
<td>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="seciliOgrenciIdler" value="@ogrenci.Id" id="ogrenci-@ogrenci.Id">
</div>
</td>
<td>@ogrenci.Numara</td>
<td>@ogrenci.Adi</td>
<td>@ogrenci.Soyadi</td>
</tr>
}
</tbody>
</table>
</div>
<div class="mt-3">
<button type="submit" class="btn btn-success">
<i class="fa fa-plus-circle"></i> Ekle
</button>
</div>
}
else
{
<div class="alert alert-info">
Eklenebilecek öğrenci bulunmamaktadır.
</div>
}
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header bg-success text-white">
<h5 class="card-title mb-0">Sınıftaki Öğrenciler</h5>
</div>
<div class="card-body">
<form asp-action="OgrenciCikar" method="post">
<input type="hidden" name="sinifId" value="@Model.SinifId" />
@if (Model.SiniftakiOgrenciler.Any())
{
<div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
<table class="table table-striped table-bordered">
<thead class="table-light sticky-top">
<tr>
<th>Seç</th>
<th>Numara</th>
<th>Adı</th>
<th>Soyadı</th>
</tr>
</thead>
<tbody>
@foreach (var sinifListe in Model.SiniftakiOgrenciler)
{
<tr>
<td>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="seciliOgrenciIdler" value="@sinifListe.OgrenciId" id="sinifta-@sinifListe.OgrenciId">
</div>
</td>
<td>@sinifListe.Ogrenci.Numara</td>
<td>@sinifListe.Ogrenci.Adi</td>
<td>@sinifListe.Ogrenci.Soyadi</td>
</tr>
}
</tbody>
</table>
</div>
<div class="mt-3">
<button type="submit" class="btn btn-danger">
<i class="fa fa-minus-circle"></i> Kaldır
</button>
</div>
}
else
{
<div class="alert alert-info">
Sınıfta öğrenci bulunmamaktadır.
</div>
}
</form>
</div>
</div>
</div>
</div>
<div class="mt-4">
<a asp-action="Index" class="btn btn-secondary">
<i class="fa fa-arrow-left"></i> Sınıf Listesine Dön
</a>
</div>
@section Scripts {
<script>
$(document).ready(function () {
// Select all checkboxes
$('.select-all').on('click', function() {
var target = $(this).data('target');
$(target).prop('checked', $(this).prop('checked'));
});
});
</script>
}