Bearbeitung von Lektion und Kurs
This commit is contained in:
111
MultipleChoiceTrainer/Controllers/SectionsController.cs
Normal file
111
MultipleChoiceTrainer/Controllers/SectionsController.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
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;
|
||||
using MultipleChoiceTrainer.Models.DataModels;
|
||||
|
||||
namespace MultipleChoiceTrainer.Controllers
|
||||
{
|
||||
public class SectionsController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public SectionsController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: Sections/Create
|
||||
public IActionResult Create(int? categoryId)
|
||||
{
|
||||
if(!categoryId.HasValue)
|
||||
{
|
||||
categoryId = _context.Categories.First().Id;
|
||||
}
|
||||
|
||||
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", categoryId);
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Sections/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,CategoryId")] Section section)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
_context.Add(section);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction(nameof(Index), "Home", new { categoryId = section.CategoryId });
|
||||
}
|
||||
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", section.CategoryId);
|
||||
return View(section);
|
||||
}
|
||||
|
||||
// GET: Sections/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var section = await _context.Sections.FindAsync(id);
|
||||
if (section == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", section.CategoryId);
|
||||
return View(section);
|
||||
}
|
||||
|
||||
// POST: Sections/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,CategoryId")] Section section)
|
||||
{
|
||||
if (id != section.Id)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Update(section);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!SectionExists(section.Id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return RedirectToAction(nameof(Index), "Home", new { categoryId = section.CategoryId });
|
||||
}
|
||||
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", section.CategoryId);
|
||||
return View(section);
|
||||
}
|
||||
|
||||
|
||||
private bool SectionExists(int id)
|
||||
{
|
||||
return _context.Sections.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user