From 6d84a4614b911d37a4b497290584d75dc41b0671 Mon Sep 17 00:00:00 2001 From: Trond Schertel Date: Tue, 9 Jun 2020 16:59:13 +0200 Subject: [PATCH] Erste Version der Fragen-Eingabe --- .../Controllers/QuestionsController.cs | 105 ++++++++++++++++++ .../Controllers/SectionsController.cs | 2 + .../Models/DataModels/Question.cs | 5 +- .../Views/Categories/Edit.cshtml | 2 +- .../Views/Home/_homeLogInPartial.cshtml | 2 +- .../Views/Questions/Create.cshtml | 57 ++++++++++ .../Views/Questions/Edit.cshtml | 33 ++++++ .../Views/Sections/Create.cshtml | 2 +- .../Views/Sections/Edit.cshtml | 2 +- 9 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 MultipleChoiceTrainer/Controllers/QuestionsController.cs create mode 100644 MultipleChoiceTrainer/Views/Questions/Create.cshtml create mode 100644 MultipleChoiceTrainer/Views/Questions/Edit.cshtml diff --git a/MultipleChoiceTrainer/Controllers/QuestionsController.cs b/MultipleChoiceTrainer/Controllers/QuestionsController.cs new file mode 100644 index 0000000..474b128 --- /dev/null +++ b/MultipleChoiceTrainer/Controllers/QuestionsController.cs @@ -0,0 +1,105 @@ +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 QuestionsController : Controller + { + private readonly ApplicationDbContext _context; + + public QuestionsController(ApplicationDbContext context) + { + _context = context; + } + + // GET: Questions/Create + public IActionResult Create(int sectionId) + { + ViewData["Section"] = _context.Sections.Include(e => e.Category).FirstOrDefault(s => s.Id == sectionId); + return View(); + } + + // POST: Questions/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 Create(Question question) + { + if (ModelState.IsValid) + { + _context.Add(question); + await _context.SaveChangesAsync(); + var section = _context.Sections.FirstOrDefault(s => s.Id == question.SectionId); + return RedirectToAction(nameof(Index), "Home", new { categoryId = section.CategoryId }); + } + ViewData["Section"] = _context.Sections.Include(e => e.Category).FirstOrDefault(s => s.Id == question.SectionId); + return View(question); + } + + // GET: Questions/Edit/5 + public async Task Edit(int? id) + { + if (id == null) + { + return NotFound(); + } + + var question = await _context.Questions.FindAsync(id); + if (question == null) + { + return NotFound(); + } + ViewData["Section"] = _context.Sections.Include(e => e.Category).FirstOrDefault(s => s.Id == question.SectionId); + return View(question); + } + + // POST: Questions/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 Edit(int id, [Bind("Id,Text,SectionId")] Question question) + { + if (id != question.Id) + { + return NotFound(); + } + + if (ModelState.IsValid) + { + try + { + _context.Update(question); + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!QuestionExists(question.Id)) + { + return NotFound(); + } + else + { + throw; + } + } + return RedirectToAction(nameof(Index), "Home", new { categoryId = question.Section.CategoryId }); + } + ViewData["Section"] = _context.Sections.Include(e => e.Category).FirstOrDefault(s => s.Id == question.SectionId); + return View(question); + } + + private bool QuestionExists(int id) + { + return _context.Questions.Any(e => e.Id == id); + } + } +} diff --git a/MultipleChoiceTrainer/Controllers/SectionsController.cs b/MultipleChoiceTrainer/Controllers/SectionsController.cs index 3e676e5..9c19160 100644 --- a/MultipleChoiceTrainer/Controllers/SectionsController.cs +++ b/MultipleChoiceTrainer/Controllers/SectionsController.cs @@ -29,6 +29,7 @@ namespace MultipleChoiceTrainer.Controllers } ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", categoryId); + ViewData["ReturnId"] = categoryId; return View(); } @@ -46,6 +47,7 @@ namespace MultipleChoiceTrainer.Controllers return RedirectToAction(nameof(Index), "Home", new { categoryId = section.CategoryId }); } ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", section.CategoryId); + ViewData["ReturnId"] = section.CategoryId; return View(section); } diff --git a/MultipleChoiceTrainer/Models/DataModels/Question.cs b/MultipleChoiceTrainer/Models/DataModels/Question.cs index 96dd5c8..6f02646 100644 --- a/MultipleChoiceTrainer/Models/DataModels/Question.cs +++ b/MultipleChoiceTrainer/Models/DataModels/Question.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; @@ -8,12 +9,14 @@ namespace MultipleChoiceTrainer.Models.DataModels public class Question { public int Id { get; set; } + [Display(Name="Frage")] public string Text { get; set; } public Section Section { get; set; } public int SectionId { get; set; } - public ICollection Choices { get; set; } = new HashSet(); + [Display(Name = "Antwortmöglichkeiten")] + public List Choices { get; set; } public ICollection Answers { get; set; } = new HashSet(); } diff --git a/MultipleChoiceTrainer/Views/Categories/Edit.cshtml b/MultipleChoiceTrainer/Views/Categories/Edit.cshtml index 7b2633a..19748bd 100644 --- a/MultipleChoiceTrainer/Views/Categories/Edit.cshtml +++ b/MultipleChoiceTrainer/Views/Categories/Edit.cshtml @@ -23,7 +23,7 @@
- Abbrechen + Abbrechen
diff --git a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml index bc1aa78..3599e07 100644 --- a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml +++ b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml @@ -33,7 +33,7 @@

Lektion bearbeiten     - Frage hinzufügen     + Frage hinzufügen     Quiz starten

diff --git a/MultipleChoiceTrainer/Views/Questions/Create.cshtml b/MultipleChoiceTrainer/Views/Questions/Create.cshtml new file mode 100644 index 0000000..d5831d2 --- /dev/null +++ b/MultipleChoiceTrainer/Views/Questions/Create.cshtml @@ -0,0 +1,57 @@ +@model MultipleChoiceTrainer.Models.DataModels.Question + +@{ + ViewData["Title"] = "Frage Anlegen"; +} + +

Frage anlegen

+

in Lektion @ViewBag.Section.Name, Kurs @ViewBag.Section.Category.Name

+
+
+
+
+
+ +
+ + + +
+
+ + @if (Model != null && Model.Choices != null) + { + @for (int i = 0; i < Model.Choices.Count; i++) + { +
+ + +
+ } + } + else + { + + + + + + + + } +
+
+ + Abbrechen +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/MultipleChoiceTrainer/Views/Questions/Edit.cshtml b/MultipleChoiceTrainer/Views/Questions/Edit.cshtml new file mode 100644 index 0000000..0e737b1 --- /dev/null +++ b/MultipleChoiceTrainer/Views/Questions/Edit.cshtml @@ -0,0 +1,33 @@ +@model MultipleChoiceTrainer.Models.DataModels.Question + +@{ + ViewData["Title"] = "Frage bearbeiten"; +} + +

Frage bearbeiten

+

in Lektion @ViewBag.Section.Name, Kurs @ViewBag.Section.Category.Name

+
+
+
+
+
+ + +
+ +
+
+ + Abbrechen +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/MultipleChoiceTrainer/Views/Sections/Create.cshtml b/MultipleChoiceTrainer/Views/Sections/Create.cshtml index 17aff85..79a1595 100644 --- a/MultipleChoiceTrainer/Views/Sections/Create.cshtml +++ b/MultipleChoiceTrainer/Views/Sections/Create.cshtml @@ -26,7 +26,7 @@
- Abbrechen + Abbrechen
diff --git a/MultipleChoiceTrainer/Views/Sections/Edit.cshtml b/MultipleChoiceTrainer/Views/Sections/Edit.cshtml index c3674d9..3eecdf8 100644 --- a/MultipleChoiceTrainer/Views/Sections/Edit.cshtml +++ b/MultipleChoiceTrainer/Views/Sections/Edit.cshtml @@ -28,7 +28,7 @@
- Abbrechen + Abbrechen