Erste Version der Fragen-Eingabe
This commit is contained in:
105
MultipleChoiceTrainer/Controllers/QuestionsController.cs
Normal file
105
MultipleChoiceTrainer/Controllers/QuestionsController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user