diff --git a/Pages/Demos/FormAction.cshtml b/Pages/Demos/FormAction.cshtml index 396beab..98846df 100644 --- a/Pages/Demos/FormAction.cshtml +++ b/Pages/Demos/FormAction.cshtml @@ -1,7 +1,6 @@ @page @model DemoWeb.Pages.Demos.FormAction - @{ Layout = "Shared/_Layout"; } @@ -9,6 +8,13 @@
+
The following problems occurred when submitting the form:
+
+ + + +
+
\ No newline at end of file diff --git a/Pages/Demos/FormAction.cshtml.cs b/Pages/Demos/FormAction.cshtml.cs index 6b0af33..f2dd104 100644 --- a/Pages/Demos/FormAction.cshtml.cs +++ b/Pages/Demos/FormAction.cshtml.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using System.ComponentModel.DataAnnotations; namespace DemoWeb.Pages.Demos; @@ -8,6 +9,15 @@ public class FormAction : PageModel [TempData] public string? StatusMessage { get; set; } + [BindProperty] + public InputModel Input { get; set; } = new(); + + public class InputModel + { + [Display(Name = "Name")] + [Required, StringLength(50)] + public string? Name { get; set; } + } public IActionResult OnPostSubmitAsync() { @@ -22,4 +32,11 @@ public IActionResult OnPostSave() return RedirectToPage(); } + + public IActionResult OnPost() + { + StatusMessage = "The button with no formaction was clicked"; + + return RedirectToPage(); + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d79c30f..f7380ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -796,6 +796,7 @@ export class ValidationService { // Because the submitter is not propagated when calling // form.submit(), we recreate it here. const submitter = submitEvent.submitter; + const initialFormAction = form.action; if (submitter) { const name = submitter.getAttribute('name'); // If name is null, a submit button is not submitted. @@ -806,9 +807,18 @@ export class ValidationService { submitterInput.value = submitter.getAttribute('value'); form.appendChild(submitterInput) } + + const formAction = submitter.getAttribute('formaction'); + if (formAction) { + form.action = formAction; + } } - form.submit(); + try { + form.submit(); + } finally { + form.action = initialFormAction; + } } } @@ -825,7 +835,7 @@ export class ValidationService { let invalidFormInputUIDs = formInputUIDs.filter(uid => this.summary[uid]); if (invalidFormInputUIDs.length > 0) { - var firstInvalid = this.elementByUID[invalidFormInputUIDs[0]] as HTMLElement; + const firstInvalid = this.elementByUID[invalidFormInputUIDs[0]] as HTMLElement; if (firstInvalid) { firstInvalid.focus(); }