Erste Version der Fragen-Eingabe

This commit is contained in:
2020-06-09 16:59:13 +02:00
parent 01585fc9ef
commit 6d84a4614b
9 changed files with 205 additions and 5 deletions

View File

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

View File

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

View File

@@ -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<Choice> Choices { get; set; } = new HashSet<Choice>();
[Display(Name = "Antwortmöglichkeiten")]
public List<Choice> Choices { get; set; }
public ICollection<Answer> Answers { get; set; } = new HashSet<Answer>();
}

View File

@@ -23,7 +23,7 @@
</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>
<a asp-action="Index" asp-controller="Home" asp-route-categoryId="@Model.Entity.Id" class="btn btn-outline-danger"><i class="fas fa-times"></i> Abbrechen</a>
</div>
</form>
</div>

View File

@@ -33,7 +33,7 @@
</div>
<p class="text-black-50">
<a asp-action="edit" asp-controller="Sections" asp-route-id="@sub.Id" class="text-black-50"><i class="far fa-edit"></i> Lektion bearbeiten</a> &nbsp;&nbsp;&nbsp;
<a asp-action="create" asp-controller="Sections" asp-route-categoryId="@category.Id" class="text-black-50"><i class="far fa-plus"></i> Frage hinzufügen</a> &nbsp;&nbsp;&nbsp;
<a asp-action="create" asp-controller="Questions" asp-route-sectionId="@sub.Id" class="text-black-50"><i class="far fa-plus"></i> Frage hinzufügen</a> &nbsp;&nbsp;&nbsp;
<a asp-action="create" asp-controller="Sections" asp-route-categoryId="@category.Id" class="text-black-50"><i class="far fa-map-marker-question"></i> Quiz starten</a>
</p>
</ul>

View File

@@ -0,0 +1,57 @@
@model MultipleChoiceTrainer.Models.DataModels.Question
@{
ViewData["Title"] = "Frage Anlegen";
}
<h4>Frage anlegen</h4>
<p class="text-black-50">in Lektion <i>@ViewBag.Section.Name</i>, Kurs <i>@ViewBag.Section.Category.Name</i></p>
<hr />
<div class="row">
<div class="col-md-12">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="SectionId" value="@ViewBag.Section.Id" />
<div class="form-group">
<label asp-for="Text" class="control-label"></label>
<input asp-for="Text" class="form-control" />
<span asp-validation-for="Text" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Choices" class="control-label"></label>
@if (Model != null && Model.Choices != null)
{
@for (int i = 0; i < Model.Choices.Count; i++)
{
<div class="text-center">
<input asp-for="@Model.Choices[i].Text" class="form-control" />
<input asp-for="@Model.Choices[i].IsTrue" class="form-control" />
</div>
}
}
else
{
<input type="hidden" name="Choices.Index" value="10" />
<input type="text" name="Choices[10].Text" class="form-control" />
<input type="checkbox" name="Choices[10].IsTrue" class="form-control" value="true" />
<input type="hidden" name="Choices.Index" value="11" />
<input type="text" name="Choices[11].Text" class="form-control" />
<input type="checkbox" name="Choices[11].IsTrue" class="form-control" value="true" />
}
</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" asp-route-categoryId="@ViewBag.Section.CategoryId" class="btn btn-outline-danger"><i class="fas fa-times"></i> Abbrechen</a>
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View File

@@ -0,0 +1,33 @@
@model MultipleChoiceTrainer.Models.DataModels.Question
@{
ViewData["Title"] = "Frage bearbeiten";
}
<h4>Frage bearbeiten</h4>
<p class="text-black-50">in Lektion <i>@ViewBag.Section.Name</i>, Kurs <i>@ViewBag.Section.Category.Name</i></p>
<hr />
<div class="row">
<div class="col-md-12">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<input type="hidden" asp-for="SectionId" />
<div class="form-group">
<label asp-for="Text" class="control-label"></label>
</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" asp-route-categoryId="@ViewBag.Section.CategoryId" class="btn btn-outline-danger"><i class="fas fa-times"></i> Abbrechen</a>
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View File

@@ -26,7 +26,7 @@
</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>
<a asp-action="Index" asp-controller="Home" asp-route-categoryId="@ViewBag.ReturnId" class="btn btn-outline-danger"><i class="fas fa-times"></i> Abbrechen</a>
</div>
</form>
</div>

View File

@@ -28,7 +28,7 @@
</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>
<a asp-action="Index" asp-controller="Home" asp-route-categoryId="@Model.CategoryId" class="btn btn-outline-danger"><i class="fas fa-times"></i> Abbrechen</a>
</div>
</form>
</div>