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);
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
@@ -8,7 +9,10 @@ namespace MultipleChoiceTrainer.Models.DataModels
public class Category
{
public int Id { get; set; }
[Display(Name="Name")]
public string Name { get; set; }
[Display(Name = "Beschreibung")]
public string Description { get; set; }
public ICollection<Section> Sections { get; set; } = new HashSet<Section>();

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
@@ -8,9 +9,13 @@ namespace MultipleChoiceTrainer.Models.DataModels
public class Section
{
public int Id { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Beschreibung")]
public string Description { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; }

View File

@@ -10,8 +10,10 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,33 @@
@model MultipleChoiceTrainer.Models.DataModels.Category
@{
ViewData["Title"] = "Neuer Kurs";
}
<h4>Kurs erstellen</h4>
<hr />
<div class="row">
<div class="col-12">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary"><i class="fas fa-save"></i> Speichern</button>
<a asp-action="Index" asp-controller="Home" class="btn btn-outline-danger"><i class="fas fa-times"></i> Abbrechen</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View File

@@ -0,0 +1,34 @@
@model MultipleChoiceTrainer.Models.DataModels.Category
@{
ViewData["Title"] = "Kurs bearbeiten";
}
<h4>Kurs bearbeiten</h4>
<hr />
<div class="row">
<div class="col-12">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary"><i class="fas fa-save"></i> Speichern</button>
<a asp-action="Index" asp-controller="Home" class="btn btn-outline-danger"><i class="fas fa-times"></i> Abbrechen</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View File

@@ -1,20 +1,20 @@
@model IList<MultipleChoiceTrainer.Models.DataModels.Category>
<p class="text-right"><i class="fal fa-plus"></i> Kategorie hinzufügen</p>
<p class="text-right"><a href="@Url.Action("Create", "Categories")" class="btn btn-link"><i class="fal fa-plus"></i> Kurs hinzufügen</a></p>
<table class="table">
@foreach (var category in Model)
{
<tr class="table-primary"><td title="@category.Description">category.Name</td></tr>
<tr class="table-primary">
<td>
<span class="badge badge-light"><a asp-action="edit" asp-controller="Categories" asp-route-id="@category.Id"><i class="far fa-file-edit"></i></a></span>
@category.Name<small class="text-muted"> @category.Description</small>
</td>
</tr>
@foreach (var sub in category.Sections)
{
<tr><td title="@sub.Description">@sub.Name</td></tr>
<tr><td title="@sub.Description">@sub.Name</td></tr>
}
}
<tr class="table-primary"><td>Hundetrainer</td></tr>
<tr><td>Lektion 1 - Der Hund</td></tr>
<tr><td>Lektion 2 - Der Schwanz</td></tr>
<tr class="table-primary"><td>Touristikmanagement</td></tr>
<tr><td>Lektion 1 - Urlaub</td></tr>
<tr><td>Lektion 2 - Reisen mit dem kranken Hund</td></tr>
<tr class="table-primary"><td>Ernährungsberatung</td></tr>
<tr><td>Lektion 1 - Zu viel essen - ist das möglich?</td></tr>
</table>