Bearbeitung von Fragen
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user