Dersler işlemi için aynı yapıyı izleyerek gerekli değişiklikleri ve CRUD işlemlerini gerçekleştirebiliriz. Aşağıda Dersler için gerekli adımlar ve dosya yapısı yer alıyor.
Controllers/Admin/DersController.cs dosyasını oluşturarak CRUD işlemlerini gerçekleştirelim.
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SchoolManagement.Data;
using SchoolManagement.Models;
namespace SchoolManagement.Controllers.Admin
{
[Authorize(Roles = "Admin")] // Sadece admin kullanıcıları erişebilecek
public class DersController : Controller
{
private readonly ApplicationDbContext _context;
public DersController(ApplicationDbContext context)
{
_context = context;
}
// Dersleri Listeleme
public async Task<IActionResult> Index(string searchString)
{
var dersler = from d in _context.Dersler
select d;
// Eğer arama yapılıyorsa, arama kriterine göre dersleri filtreliyoruz
if (!string.IsNullOrEmpty(searchString))
{
dersler = dersler.Where(d => d.DersAdi.Contains(searchString));
}
return View("~/Views/Admin/Ders/Index.cshtml", await dersler.ToListAsync());
}
// Yeni Ders Ekleme (GET)
public IActionResult Create()
{
return View("~/Views/Admin/Ders/Create.cshtml");
}
// Yeni Ders Ekleme (POST)
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Ders ders)
{
if (ModelState.IsValid)
{
_context.Add(ders);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); // Dersler sayfasına yönlendiriyoruz
}
return View("~/Views/Admin/Ders/Create.cshtml", ders); // Hata varsa aynı sayfayı tekrar gösteriyoruz
}
// Ders Düzenleme (GET)
public async Task<IActionResult> Edit(int id)
{
var ders = await _context.Dersler.FindAsync(id);
if (ders == null) return NotFound(); // Ders bulunamazsa hata sayfasına yönlendir
return View("~/Views/Admin/Ders/Edit.cshtml", ders);
}
// Ders Düzenleme (POST)
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, Ders ders)
{
if (id != ders.Id) return NotFound(); // ID uyuşmazsa hata sayfasına yönlendir
if (ModelState.IsValid)
{
_context.Update(ders);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); // Dersler sayfasına yönlendir
}
return View("~/Views/Admin/Ders/Edit.cshtml", ders); // Hata varsa aynı sayfayı tekrar göster
}
// Ders Silme (GET)
public async Task<IActionResult> Delete(int id)
{
var ders = await _context.Dersler.FindAsync(id);
if (ders == null) return NotFound(); // Ders bulunamazsa hata sayfasına yönlendir
return View("~/Views/Admin/Ders/Delete.cshtml", ders);
}
// Ders Silme (POST)
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var ders = await _context.Dersler.FindAsync(id);
if (ders != null)
{
_context.Dersler.Remove(ders);
await _context.SaveChangesAsync();
}
return RedirectToAction(nameof(Index)); // Dersler sayfasına yönlendir
}
}
}
Menüde, Dersler için yeni bir buton ekleyelim. Bu buton, DersController'ın Index action'ına yönlendirecek.
<li><a class="dropdown-item" asp-controller="Ders" asp-action="Index">Dersler</a></li>
Derslerin listeleneceği ana sayfayı Views/Admin/Ders/Index.cshtml dosyasına ekleyelim.
@model IEnumerable<SchoolManagement.Models.Ders>
@{
ViewData["Title"] = "Dersler";
}
<h2>Ders Listesi</h2>
<p>
<a class="btn btn-primary" asp-action="Create">Yeni Ders Ekle</a>
</p>
<form asp-action="Index" method="get">
<input type="text" name="searchString" class="form-control w-25 d-inline" placeholder="Ara..." />
<button type="submit" class="btn btn-secondary">Ara</button>
</form>
<table class="table table-bordered mt-3">
<thead>
<tr>
<th>ID</th>
<th>Ders Adı</th>
<th>İşlemler</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.DersAdi</td>
<td>
<a class="btn btn-warning btn-sm" asp-action="Edit" asp-route-id="@item.Id">Düzenle</a>
<a class="btn btn-danger btn-sm" asp-action="Delete" asp-route-id="@item.Id">Sil</a>
</td>
</tr>
}
</tbody>
</table>
Yeni Ders eklemek için Views/Admin/Ders/Create.cshtml dosyasını oluşturacağız.
@model SchoolManagement.Models.Ders
@{
ViewData["Title"] = "Yeni Ders Ekle";
}
<h2>Yeni Ders Ekle</h2>
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="DersAdi"></label>
<input asp-for="DersAdi" class="form-control" required />
</div>
<button type="submit" class="btn btn-success mt-2">Kaydet</button>
</form>
<p>
<a asp-action="Index">Geri Dön</a>
</p>
Ders düzenleme işlemi için Views/Admin/Ders/Edit.cshtml dosyasını ekleyelim.
@model SchoolManagement.Models.Ders
@{
ViewData["Title"] = "Ders Düzenle";
}
<h2>Ders Düzenle</h2>
<form asp-action="Edit" method="post">
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="DersAdi"></label>
<input asp-for="DersAdi" class="form-control" required />
</div>
<button type="submit" class="btn btn-primary mt-2">Güncelle</button>
</form>
<p>
<a asp-action="Index">Geri Dön</a>
</p>
Ders silme işlemi için Views/Admin/Ders/Delete.cshtml dosyasını oluşturalım.
@model SchoolManagement.Models.Ders
@{
ViewData["Title"] = "Ders Sil";
}
<h2>Ders Sil</h2>
<p>@Model.DersAdi adlı dersi silmek istediğinizden emin misiniz?</p>
<form asp-action="Delete" method="post">
<input type="hidden" asp-for="Id" />
<button type="submit" class="btn btn-danger">Sil</button>
</form>
<p>
<a asp-action="Index">Geri Dön</a>
</p>