From f2dfc90de8aec4dc36ea8aefa06daa98d9861074 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Mon, 16 Jan 2023 02:38:13 -0600 Subject: [PATCH] Added ability to add alerts onto the page via controller --- LANCommander/Controllers/BaseController.cs | 31 ++++++++++++++ LANCommander/Models/AlertViewModel.cs | 6 +++ LANCommander/Views/Shared/_Layout.cshtml | 47 +++++++++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 LANCommander/Controllers/BaseController.cs create mode 100644 LANCommander/Models/AlertViewModel.cs diff --git a/LANCommander/Controllers/BaseController.cs b/LANCommander/Controllers/BaseController.cs new file mode 100644 index 0000000..95a1b5e --- /dev/null +++ b/LANCommander/Controllers/BaseController.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text.Json; + +namespace LANCommander.Controllers +{ + public class BaseController : Controller + { + public void Alert(string message, string type = "info", bool dismissable = true) + { + List alerts; + + try + { + alerts = JsonSerializer.Deserialize>((string)TempData["Alerts"]); + } + catch + { + alerts = new List(); + } + + alerts.Add(new AlertViewModel() + { + Message = message, + Type = type, + Dismissable = dismissable + }); + + TempData["Alerts"] = JsonSerializer.Serialize(alerts); + } + } +} diff --git a/LANCommander/Models/AlertViewModel.cs b/LANCommander/Models/AlertViewModel.cs new file mode 100644 index 0000000..43a3aff --- /dev/null +++ b/LANCommander/Models/AlertViewModel.cs @@ -0,0 +1,6 @@ +public class AlertViewModel +{ + public string Message { get; set; } + public bool Dismissable { get; set; } + public string Type { get; set; } +} \ No newline at end of file diff --git a/LANCommander/Views/Shared/_Layout.cshtml b/LANCommander/Views/Shared/_Layout.cshtml index 7bcd38f..3815d21 100644 --- a/LANCommander/Views/Shared/_Layout.cshtml +++ b/LANCommander/Views/Shared/_Layout.cshtml @@ -1,4 +1,5 @@ -@inject Microsoft.AspNetCore.Http.IHttpContextAccessor _HttpContext +@using System.Text.Json +@inject Microsoft.AspNetCore.Http.IHttpContextAccessor _HttpContext @{ _HttpContext.HttpContext.Response.Headers["Cache-Control"] = "no-store"; } @@ -93,6 +94,31 @@ +
+ @if (TempData["Alerts"] != null) + { + try + { + foreach (var alert in JsonSerializer.Deserialize>((string)TempData["Alerts"])) + { + + } + } + catch { } + } +
+ @@ -100,6 +126,25 @@ + + @await RenderSectionAsync("Scripts", required: false)