Kursanlage und bearbeitung
This commit is contained in:
118
MultipleChoiceTrainer/Controllers/CategoriesController.cs
Normal file
118
MultipleChoiceTrainer/Controllers/CategoriesController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user