From c7dfcf918fbcca7ff6fa569f03c37a94b74ddb28 Mon Sep 17 00:00:00 2001 From: Tokk Date: Wed, 10 Jun 2020 19:27:01 +0200 Subject: [PATCH] Fehlerbehebung bei leerer DB; --- MultipleChoiceTrainer/Controllers/HomeController.cs | 2 +- MultipleChoiceTrainer/Models/HomeViewModel.cs | 2 +- .../Views/Home/_homeLogInPartial.cshtml | 13 +++++++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/MultipleChoiceTrainer/Controllers/HomeController.cs b/MultipleChoiceTrainer/Controllers/HomeController.cs index df4ccde..fb989ab 100644 --- a/MultipleChoiceTrainer/Controllers/HomeController.cs +++ b/MultipleChoiceTrainer/Controllers/HomeController.cs @@ -30,7 +30,7 @@ namespace MultipleChoiceTrainer.Controllers if (signInManager.IsSignedIn(User)) { var categories = context.Categories.Include(e => e.Sections).ThenInclude(e => e.Questions).ToList(); - var selectedCategory = categoryId.HasValue ? categoryId.Value : categories.First().Id; + var selectedCategory = categoryId ?? (categories.FirstOrDefault()?.Id); return View(new HomeViewModel() { Categories = categories, SelectedCategoryId = selectedCategory }); } return View(); diff --git a/MultipleChoiceTrainer/Models/HomeViewModel.cs b/MultipleChoiceTrainer/Models/HomeViewModel.cs index 543b464..36f2562 100644 --- a/MultipleChoiceTrainer/Models/HomeViewModel.cs +++ b/MultipleChoiceTrainer/Models/HomeViewModel.cs @@ -9,6 +9,6 @@ namespace MultipleChoiceTrainer.Models public class HomeViewModel { public IList Categories { get; set; } - public int SelectedCategoryId { get; set; } + public int? SelectedCategoryId { get; set; } } } diff --git a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml index f3bfae6..7b216ef 100644 --- a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml +++ b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml @@ -20,13 +20,15 @@

Kurs bearbeiten     Lektion hinzufügen     - Quiz starten + @if (category.Sections.Any(e => e.Questions.Any())) { + Quiz starten + }

@foreach (var sub in category.Sections) { - +
}