From de7379a33c2c8af0c2b735709135946f20217b3a Mon Sep 17 00:00:00 2001 From: Denis-RZ <77514212+Denis-RZ@users.noreply.github.com> Date: Wed, 18 Jun 2025 14:20:59 +0800 Subject: [PATCH] Enhance admin block UI --- .../AdminBlockTemplateController.cs | 49 ++++++++++++------- .../Views/AdminBlockTemplate/AddToPage.cshtml | 33 ++++++++++--- .../Views/AdminPageSection/Edit.cshtml | 4 ++ .../Views/Shared/_SectionEditor.cshtml | 11 ++--- 4 files changed, 65 insertions(+), 32 deletions(-) diff --git a/website/MyWebApp/Controllers/AdminBlockTemplateController.cs b/website/MyWebApp/Controllers/AdminBlockTemplateController.cs index 294481d..8e38870 100644 --- a/website/MyWebApp/Controllers/AdminBlockTemplateController.cs +++ b/website/MyWebApp/Controllers/AdminBlockTemplateController.cs @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Http; using System.Collections.Generic; +using System.Linq; using System.IO; using System.Text.Json; using MyWebApp.Data; @@ -160,14 +161,19 @@ public async Task AddToPage(int id) [HttpPost] [ValidateAntiForgeryToken] - public async Task AddToPage(int id, int pageId, string zone, int? roleId) + public async Task AddToPage(int id, List pageIds, string zone, string role) { var template = await _db.BlockTemplates.FindAsync(id); - var page = await _db.Pages.FindAsync(pageId); - if (template == null || page == null) + if (template == null) return NotFound(); + if (pageIds == null || pageIds.Count == 0) { - return NotFound(); + await LoadPagesAsync(); + ViewBag.BlockId = id; + ModelState.AddModelError("pageIds", "Page selection required"); + return View(); } + var roleEntity = await _db.Roles.FirstOrDefaultAsync(r => r.Name == role); + int? roleId = roleEntity?.Id; zone = zone?.Trim() ?? string.Empty; if (string.IsNullOrEmpty(zone)) { @@ -176,21 +182,28 @@ public async Task AddToPage(int id, int pageId, string zone, int? ModelState.AddModelError("zone", "Zone required"); return View(); } - var sort = await _db.PageSections - .Where(s => s.PageId == pageId && s.Zone == zone) - .Select(s => s.SortOrder) - .DefaultIfEmpty(-1) - .MaxAsync() + 1; - var section = new PageSection + if (pageIds.Contains(0)) { - PageId = pageId, - Zone = zone, - SortOrder = sort, - Html = template.Html, - Type = PageSectionType.Html, - RoleId = roleId - }; - _db.PageSections.Add(section); + pageIds = await _db.Pages.Select(p => p.Id).ToListAsync(); + } + foreach (var pageId in pageIds) + { + var sort = await _db.PageSections + .Where(s => s.PageId == pageId && s.Zone == zone) + .Select(s => s.SortOrder) + .DefaultIfEmpty(-1) + .MaxAsync() + 1; + var section = new PageSection + { + PageId = pageId, + Zone = zone, + SortOrder = sort, + Html = template.Html, + Type = PageSectionType.Html, + RoleId = roleId + }; + _db.PageSections.Add(section); + } await _db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } diff --git a/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml b/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml index c902368..f574f8d 100644 --- a/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml +++ b/website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml @@ -7,8 +7,13 @@
- - +
+
+ + - - @foreach (var r in ViewBag.Roles as List) - { - - } +
+ +@section Scripts { + +} diff --git a/website/MyWebApp/Views/AdminPageSection/Edit.cshtml b/website/MyWebApp/Views/AdminPageSection/Edit.cshtml index 05822c0..6f30388 100644 --- a/website/MyWebApp/Views/AdminPageSection/Edit.cshtml +++ b/website/MyWebApp/Views/AdminPageSection/Edit.cshtml @@ -9,6 +9,10 @@

Edit Section

+
+ + +
diff --git a/website/MyWebApp/Views/Shared/_SectionEditor.cshtml b/website/MyWebApp/Views/Shared/_SectionEditor.cshtml index f20e4bf..b6c82a7 100644 --- a/website/MyWebApp/Views/Shared/_SectionEditor.cshtml +++ b/website/MyWebApp/Views/Shared/_SectionEditor.cshtml @@ -15,12 +15,11 @@
- + + + +