Bilder zur Frage können hochgeladen werden;
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultipleChoiceTrainer.Data;
|
||||
using MultipleChoiceTrainer.Models;
|
||||
using MultipleChoiceTrainer.Models.DataModels;
|
||||
|
||||
namespace MultipleChoiceTrainer.Controllers
|
||||
@@ -13,10 +16,11 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
public class QuestionsController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public QuestionsController(ApplicationDbContext context)
|
||||
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||
public QuestionsController(ApplicationDbContext context, IWebHostEnvironment hostEnvironment)
|
||||
{
|
||||
_context = context;
|
||||
_webHostEnvironment = hostEnvironment;
|
||||
}
|
||||
|
||||
public IActionResult List(int sectionId)
|
||||
@@ -37,13 +41,14 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create(Question question)
|
||||
{
|
||||
public async Task<IActionResult> Create(QuestionViewModel question)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
{
|
||||
question.Choices = question.Choices.ToList().Where(e => !string.IsNullOrEmpty(e.Text) && !string.IsNullOrWhiteSpace(e.Text)).ToList();
|
||||
CopyUploadedFile(question);
|
||||
|
||||
_context.Add(question);
|
||||
_context.Add(question as Question);
|
||||
await _context.SaveChangesAsync();
|
||||
var section = _context.Sections.FirstOrDefault(s => s.Id == question.SectionId);
|
||||
return RedirectToAction(nameof(Index), "Home", new { categoryId = section.CategoryId });
|
||||
@@ -66,7 +71,16 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
return NotFound();
|
||||
}
|
||||
ViewData["Section"] = _context.Sections.Include(e => e.Category).FirstOrDefault(s => s.Id == question.SectionId);
|
||||
return View(question);
|
||||
|
||||
var vm = new QuestionViewModel()
|
||||
{
|
||||
Choices = question.Choices,
|
||||
Image = question.Image,
|
||||
Id = question.Id,
|
||||
SectionId = question.SectionId,
|
||||
Text = question.Text
|
||||
};
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
// POST: Questions/Edit/5
|
||||
@@ -74,7 +88,7 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(int id, Question question)
|
||||
public async Task<IActionResult> Edit(int id, QuestionViewModel question)
|
||||
{
|
||||
if (id != question.Id)
|
||||
{
|
||||
@@ -87,9 +101,10 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
|
||||
question.Choices = question.Choices.ToList().Where(e => !string.IsNullOrEmpty(e.Text) && !string.IsNullOrWhiteSpace(e.Text)).ToList();
|
||||
|
||||
CopyUploadedFile(question);
|
||||
try
|
||||
{
|
||||
_context.Update(question);
|
||||
_context.Update(question as Question);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
if(deletedChoices.Any())
|
||||
@@ -120,5 +135,21 @@ namespace MultipleChoiceTrainer.Controllers
|
||||
{
|
||||
return _context.Questions.Any(e => e.Id == id);
|
||||
}
|
||||
|
||||
private void CopyUploadedFile(QuestionViewModel model)
|
||||
{
|
||||
if (model.QuestionImage != null)
|
||||
{
|
||||
string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "uploads", "images", "questions");
|
||||
Directory.CreateDirectory(uploadsFolder);
|
||||
|
||||
model.Image = Guid.NewGuid().ToString() + Path.GetExtension(model.QuestionImage.FileName);
|
||||
string filePath = Path.Combine(uploadsFolder, model.Image);
|
||||
using (var fileStream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
model.QuestionImage.CopyTo(fileStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user