From a301a2a168d810e3eae0a126ae01bf2bc79cf651 Mon Sep 17 00:00:00 2001 From: ivanszabo Date: Thu, 13 Mar 2025 13:53:16 +0100 Subject: [PATCH] =?UTF-8?q?Na=20most=20elvileg=20j=C3=B3=20a=20saj=C3=A1t?= =?UTF-8?q?=20form=20postdata=20cucc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CheckPoints/CheckPointEditPage.cshtml | 55 ++++++++++++++++--- .../CheckPoints/CheckPointEditPage.cshtml.cs | 6 ++ .../CheckPoints/CheckPointsPage.cshtml | 9 +-- src/WorkFlowCheck.Web/Program.cs | 4 ++ src/WorkFlowCheck.Web/wwwroot/js/site.js | 27 +++++++++ 5 files changed, 88 insertions(+), 13 deletions(-) diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml index bf2f677..ae1d20a 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml @@ -1,36 +1,44 @@ @page @model WorkFlowCheck.Web.Pages.BaseStock.CheckPoints.CheckPointEditPageModel +@using Microsoft.AspNetCore.Antiforgery +@inject IAntiforgery Antiforgery @{ ViewData["Title"] = "Ellenőrzési pont"; var checkpointId = Model.CheckPoint.Id; var url = Url.Page("./CheckPointEditPage", "LoadCheckListTemplateRows", new { id = checkpointId }); + var urlPost = Url.Page("./CheckPointEditPage", "Save"); }

@ViewData["Title"]

+ + +
+
+ -
- -
+
-
+
- +
+ +
-
- -
-
+
+
+ +
@@ -97,5 +105,34 @@ const row = table.row($(this).closest('tr')).data(); }); + $("#saveCheckPoint").click(function () { + const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); + var formData = getFormAsNestedObject('#checkPointForm'); + + console.log(formData); + console.log(JSON.stringify(formData.CheckPoint)); + + $.ajax({ + url: $('#checkPointEditPostUrl').val(), + type: 'POST', + headers: { + 'X-CSRF-TOKEN': csrfToken, + 'Content-Type': 'application/json' + }, + data: JSON.stringify(formData.CheckPoint), + success: function (response) { + // Sikeres válasz esetén + alert("Sikeres mentés!"); + // opcionálisan pl. form reset, redirect stb. + }, + error: function (xhr, status, error) { + // Hiba esetén + console.error("Hiba: ", error); + alert("Mentés nem sikerült."); + } + }); + + }); + } diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs index dc7206a..1a80347 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs @@ -7,6 +7,7 @@ using WorkFlowCheck.Web.Services.Interfaces; namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints { [Authorize] + [ValidateAntiForgeryToken] public class CheckPointEditPageModel : PageModel { private readonly ILogger _logger; @@ -30,5 +31,10 @@ namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints CheckPoint = await _baseStockService.GetCheckPoint(id); return new JsonResult(new { data = CheckPoint.CheckListTemplateRowDTO }); } + + public async Task OnPostSave([FromBody] CheckPointDTO checkPointDTO) + { + int i = 0; + } } } diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml index 71c14d2..785062e 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml @@ -4,10 +4,11 @@ ViewData["Title"] = "Ellenőrzési pontok"; }

@ViewData["Title"]

-
- -
-
+ +
+
+ +
diff --git a/src/WorkFlowCheck.Web/Program.cs b/src/WorkFlowCheck.Web/Program.cs index 1f5e4d4..ff24eee 100644 --- a/src/WorkFlowCheck.Web/Program.cs +++ b/src/WorkFlowCheck.Web/Program.cs @@ -26,6 +26,10 @@ builder.Host.UseSerilog(); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddSingleton(); +builder.Services.AddAntiforgery(options => +{ + options.HeaderName = "X-CSRF-TOKEN"; // JS API hívásokhoz +}); var jwtSettings = builder.Configuration.GetSection("Jwt"); diff --git a/src/WorkFlowCheck.Web/wwwroot/js/site.js b/src/WorkFlowCheck.Web/wwwroot/js/site.js index 0937657..c98a6d8 100644 --- a/src/WorkFlowCheck.Web/wwwroot/js/site.js +++ b/src/WorkFlowCheck.Web/wwwroot/js/site.js @@ -2,3 +2,30 @@ // for details on configuring this project to bundle and minify static web assets. // Write your JavaScript code. +function getFormAsNestedObject(formSelector) { + var formArray = $(formSelector).serializeArray(); + var result = {}; + + formArray.forEach(function (item) { + var keys = item.name.split('.'); + var value = item.value; + var current = result; + + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + + // Ha utolsó kulcs, értéket rendelünk hozzá + if (i === keys.length - 1) { + current[key] = value; + } else { + // Ha a következÅ‘ szint nincs meg, hozzuk létre + if (!current[key]) { + current[key] = {}; + } + current = current[key]; + } + } + }); + + return result; +} \ No newline at end of file