Bearbeitung von Fragen
This commit is contained in:
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30128.74
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultipleChoiceTrainer", "MultipleChoiceTrainer\MultipleChoiceTrainer.csproj", "{5B8B8E79-0139-4A22-A789-F9A4EBD703BA}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultipleChoiceTrainer", "MultipleChoiceTrainer\MultipleChoiceTrainer.csproj", "{5B8B8E79-0139-4A22-A789-F9A4EBD703BA}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
||||
@@ -19,6 +19,12 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public IActionResult List(int sectionId)
|
||||
{
|
||||
return View(_context.Questions.Include(e => e.Choices).Where(e => e.SectionId == sectionId));
|
||||
}
|
||||
|
||||
|
||||
// GET: Questions/Create
|
||||
public IActionResult Create(int sectionId)
|
||||
{
|
||||
@@ -35,6 +41,8 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
question.Choices = question.Choices.ToList().Where(e => !string.IsNullOrEmpty(e.Text) && !string.IsNullOrWhiteSpace(e.Text)).ToList();
|
||||
|
||||
_context.Add(question);
|
||||
await _context.SaveChangesAsync();
|
||||
var section = _context.Sections.FirstOrDefault(s => s.Id == question.SectionId);
|
||||
@@ -52,7 +60,7 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var question = await _context.Questions.FindAsync(id);
|
||||
var question = _context.Questions.Include(e => e.Choices).FirstOrDefault(e => e.Id == id);
|
||||
if (question == null)
|
||||
{
|
||||
return NotFound();
|
||||
@@ -66,7 +74,7 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
// 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)
|
||||
public async Task<IActionResult> Edit(int id, Question question)
|
||||
{
|
||||
if (id != question.Id)
|
||||
{
|
||||
@@ -75,10 +83,21 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var deletedChoices = question.Choices.Where(e => (string.IsNullOrEmpty(e.Text) || string.IsNullOrWhiteSpace(e.Text)) && e.Id != 0).Select(e => e.Id).ToList();
|
||||
|
||||
question.Choices = question.Choices.ToList().Where(e => !string.IsNullOrEmpty(e.Text) && !string.IsNullOrWhiteSpace(e.Text)).ToList();
|
||||
|
||||
try
|
||||
{
|
||||
_context.Update(question);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
if(deletedChoices.Any())
|
||||
{
|
||||
var q = _context.Questions.Include(e => e.Choices).First(e => e.Id == id);
|
||||
q.Choices.RemoveAll(e => deletedChoices.Contains(e.Id));
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
@@ -91,7 +110,7 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return RedirectToAction(nameof(Index), "Home", new { categoryId = question.Section.CategoryId });
|
||||
return RedirectToAction(nameof(List), new { sectionId = question.SectionId });
|
||||
}
|
||||
ViewData["Section"] = _context.Sections.Include(e => e.Category).FirstOrDefault(s => s.Id == question.SectionId);
|
||||
return View(question);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using MultipleChoiceTrainer.Validation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
@@ -11,6 +12,7 @@ namespace MultipleChoiceTrainer.Models.DataModels
|
||||
public int Id { get; set; }
|
||||
|
||||
[Display(Name="Name")]
|
||||
[NotNullOrEmpty]
|
||||
public string Name { get; set; }
|
||||
[Display(Name = "Beschreibung")]
|
||||
public string Description { get; set; }
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using MultipleChoiceTrainer.Validation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -10,12 +12,14 @@ namespace MultipleChoiceTrainer.Models.DataModels
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Display(Name="Frage")]
|
||||
[NotNullOrEmpty]
|
||||
public string Text { get; set; }
|
||||
|
||||
public Section Section { get; set; }
|
||||
public int SectionId { get; set; }
|
||||
|
||||
[Display(Name = "Antwortmöglichkeiten")]
|
||||
[NotEmptyChoiceCollection()]
|
||||
public List<Choice> Choices { get; set; }
|
||||
|
||||
public ICollection<Answer> Answers { get; set; } = new HashSet<Answer>();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using MultipleChoiceTrainer.Validation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
@@ -11,6 +12,7 @@ namespace MultipleChoiceTrainer.Models.DataModels
|
||||
public int Id { get; set; }
|
||||
|
||||
[Display(Name = "Name")]
|
||||
[NotNullOrEmpty]
|
||||
public string Name { get; set; }
|
||||
[Display(Name = "Beschreibung")]
|
||||
public string Description { get; set; }
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using MultipleChoiceTrainer.Models.DataModels;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MultipleChoiceTrainer.Validation
|
||||
{
|
||||
public class NotEmptyChoiceCollectionAttribute : ValidationAttribute
|
||||
{
|
||||
public NotEmptyChoiceCollectionAttribute()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override bool IsValid(object value)
|
||||
{
|
||||
ErrorMessage = "Es müssen mindestens zwei Antwortmöglichkeiten eingegeben werden!";
|
||||
|
||||
if (value == null || !(value is ICollection<Choice> collection))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(collection == null || collection.Where(e => !string.IsNullOrEmpty(e.Text)).Count() < 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!collection.Where(e => !string.IsNullOrEmpty(e.Text)).Any(e => e.IsTrue))
|
||||
{
|
||||
ErrorMessage = "Es muss mindestens eine richtige Antwort eingegeben werden!";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
28
MultipleChoiceTrainer/Validation/NotNullOrEmptyAttribute.cs
Normal file
28
MultipleChoiceTrainer/Validation/NotNullOrEmptyAttribute.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MultipleChoiceTrainer.Validation
|
||||
{
|
||||
public class NotNullOrEmptyAttribute : ValidationAttribute
|
||||
{
|
||||
public NotNullOrEmptyAttribute()
|
||||
{
|
||||
}
|
||||
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
if (value == null)
|
||||
return new ValidationResult($"{validationContext.DisplayName} darf nicht leer sein!");
|
||||
|
||||
if (string.IsNullOrEmpty(value.ToString()) || string.IsNullOrWhiteSpace(value.ToString()))
|
||||
{
|
||||
return new ValidationResult($"{validationContext.DisplayName} darf nicht leer sein!");
|
||||
}
|
||||
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@
|
||||
<ul class="list-group-item list-group-item-action">
|
||||
<div class="d-flex w-100 justify-content-between">
|
||||
<h5 class="mb-1">@sub.Name <small class="text-muted">@sub.Description</small></h5>
|
||||
<small>@sub.Questions.Count() Fragen</small>
|
||||
<small><a asp-action="list" asp-controller="questions" asp-route-sectionId="@sub.Id" class="text-decoration-none text-black-50">@sub.Questions.Count() Fragen</a></small>
|
||||
</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>
|
||||
|
||||
@@ -12,33 +12,37 @@
|
||||
<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 class="form-group row">
|
||||
<label asp-for="Text" class="control-label col-2"></label>
|
||||
<input asp-for="Text" class="form-control col-10" />
|
||||
<span asp-validation-for="Text" class="text-danger offset-2"></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="form-group row">
|
||||
<label asp-for="Choices" class="control-label col-2"></label>
|
||||
<div class="col-10 row">
|
||||
@for (int i = 0; i < 5; 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>
|
||||
<div class="row col-12 mb-1 px-0 mx-0">
|
||||
@if (Model != null && Model.Choices != null && Model.Choices.Count > i)
|
||||
{
|
||||
<input asp-for="@Model.Choices[i].Text" class="form-control col-10" />
|
||||
<div class="form-check col-2 text-right">
|
||||
<input asp-for="@Model.Choices[i].IsTrue" class="form-check-input" value="true" />
|
||||
<label asp-for="@Model.Choices[i].IsTrue" class="form-check-label">Richtig</label>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<input type="text" name="Choices[@i].Text" class="form-control col-10" />
|
||||
<div class="form-check col-2 text-right">
|
||||
<input type="checkbox" name="Choices[@i].IsTrue" id="CheckBox-IsTrue-@i" class="form-check-input" value="true" />
|
||||
<label for="CheckBox-IsTrue-@i" class="form-check-label">Richtig</label>
|
||||
</div>
|
||||
}
|
||||
</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" />
|
||||
}
|
||||
<span asp-validation-for="Choices" class="text-danger" ></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-outline-primary"><i class="fas fa-save"></i> Speichern</button>
|
||||
|
||||
@@ -13,9 +13,41 @@
|
||||
<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 class="form-group row">
|
||||
<label asp-for="Text" class="control-label col-2"></label>
|
||||
<input asp-for="Text" class="form-control col-10" />
|
||||
<span asp-validation-for="Text" class="text-danger offset-2"></span>
|
||||
</div>
|
||||
<div class="form-group row">
|
||||
<label asp-for="Choices" class="control-label col-2"></label>
|
||||
<div class="col-10 row">
|
||||
@for (int i = 0; i < 5; i++)
|
||||
{
|
||||
<div class="row col-12 mb-1 px-0 mx-0">
|
||||
@if (Model != null && Model.Choices != null && Model.Choices.Count > i)
|
||||
{
|
||||
<input asp-for="@Model.Choices[i].Id" type="hidden" />
|
||||
<input asp-for="@Model.Choices[i].Text" class="form-control col-10" />
|
||||
<div class="form-check col-2 text-right">
|
||||
<input asp-for="@Model.Choices[i].IsTrue" class="form-check-input" value="true" />
|
||||
<label asp-for="@Model.Choices[i].IsTrue" class="form-check-label">Richtig</label>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<input type="text" name="Choices[@i].Text" class="form-control col-10" />
|
||||
<div class="form-check col-2 text-right">
|
||||
<input type="checkbox" name="Choices[@i].IsTrue" id="CheckBox-IsTrue-@i" class="form-check-input" value="true" />
|
||||
<label for="CheckBox-IsTrue-@i" class="form-check-label">Richtig</label>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
<span asp-validation-for="Choices" class="text-danger" ></span>
|
||||
</div>
|
||||
</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>
|
||||
|
||||
37
MultipleChoiceTrainer/Views/Questions/List.cshtml
Normal file
37
MultipleChoiceTrainer/Views/Questions/List.cshtml
Normal file
@@ -0,0 +1,37 @@
|
||||
@model IEnumerable<MultipleChoiceTrainer.Models.DataModels.Question>
|
||||
@{
|
||||
ViewData["Title"] = "Fragen";
|
||||
}
|
||||
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.Text)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.Choices)
|
||||
</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in Model) {
|
||||
<tr>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Text)
|
||||
</td>
|
||||
<td>
|
||||
@foreach (var choice in item.Choices)
|
||||
{
|
||||
<i class="@if (choice.IsTrue) { <text>fas fa-check text-success</text> } else { <text>fas fa-times text-danger</text> }"></i> @choice.Text<br />
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
<a asp-action="Edit" asp-route-id="@item.Id"><i class="fas fa-edit"></i> Bearbeiten</a>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
Reference in New Issue
Block a user