Kursanlage und bearbeitung

This commit is contained in:
2020-06-09 13:52:25 +02:00
parent cb5acd12eb
commit 6a49e1b8f7
7 changed files with 204 additions and 8 deletions

View File

@@ -0,0 +1,118 @@
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 MultipleChoiceTrainer.Data;
using MultipleChoiceTrainer.Models.DataModels;
namespace MultipleChoiceTrainer.Controllers
{
public class CategoriesController : Controller
{
private readonly ApplicationDbContext _context;
public CategoriesController(ApplicationDbContext context)
{
_context = context;
}
// GET: Categories/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var category = await _context.Categories
.FirstOrDefaultAsync(m => m.Id == id);
if (category == null)
{
return NotFound();
}
return View(category);
}
// GET: Categories/Create
public IActionResult Create()
{
return View();
}
// POST: Categories/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,Description")] Category category)
{
if (ModelState.IsValid)
{
_context.Add(category);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index), "Home");
}
return View(category);
}
// GET: Categories/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var category = await _context.Categories.FindAsync(id);
if (category == null)
{
return NotFound();
}
return View(category);
}
// POST: Categories/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Description")] Category category)
{
if (id != category.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(category);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CategoryExists(category.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index), "Home");
}
return View(category);
}
private bool CategoryExists(int id)
{
return _context.Categories.Any(e => e.Id == id);
}
}
}