Keep in mind, another, sometimes simpler method is to make your form AJAX, where it posts through an ajax call.
I wrote a taghelper to make a form ajax-able, it also hooks up the client side validation after. Just add "ajaxify-form" onto your form tag when this is in your solution and make sure your action result is a partial view of the form.
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Threading.Tasks;
namespace KinderMorgan.Library.TagHelpers
{
[HtmlTargetElement("form", Attributes = "ajaxify-form")]
public class AjaxifyFormTagHelper : TagHelper
{
// Just so it shows up
public bool AjaxifyForm { get; set; } = true;
public string AjaxifyFormErrorMessage { get; set; } = "An error occurred while submitting.";
public string AjaxifyFormRedirectAfterSubmission { get; set; } = string.Empty;
public AjaxifyFormTagHelper()
{
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (AjaxifyForm)
{
var randomID = Guid.NewGuid().ToString().Replace("-", "");
output.PreElement.AppendHtml($"<div id=\"ajaxwrapper-{randomID}\">");
// Adds loader, will need to use styling to hide/show when form appends ajax-submitting to form
output.PreContent.AppendHtml(@"
<div class=""ajax-submitting-overlay"">
<div class=""loader""></div>
</div>");
output.PostElement.AppendHtml($"</div>");
output.PostElement.AppendHtml(@"<script type=""text/javascript"">
function AjaxifyForm" + randomID + @"() {
var formWrapper = document.getElementById('ajaxwrapper-" + randomID + @"');
var form = formWrapper.getElementsByTagName('form')[0];
var formSubmitTo = form.getAttribute('action');
var formMethod = form.getAttribute('method');
var alreadyRan = false;
form.onsubmit = function() {
var jQueryValidatorExists = typeof($) !== 'undefined' && typeof($.validator) !== 'undefined';
var jQueryUnobtrusiveExists = jQueryValidatorExists && typeof($.validator.unobtrusive) !== 'undefined';
if(jQueryValidatorExists) {
if(!$(form).valid()) {
return false;
};
}
var data = new FormData(form);
if(!alreadyRan) {
alreadyRan = true;
form.classList.add('ajax-submitting');
fetch(formSubmitTo, {
method: formMethod,
body: data
}).then(response => {
return response.text();
}).then(function (html) {
formWrapper.innerHTML = html;
var newForms = formWrapper.getElementsByTagName('form');
if(newForms.length > 0) {
AjaxifyForm" + randomID + @"();
var newForm = newForms[0];
if(jQueryValidatorExists) {
var newFormjQuery = $(newForm);
if(jQueryUnobtrusiveExists) {
$.validator.unobtrusive.parse(newFormjQuery);
newFormjQuery.validate(newFormjQuery.data('unobtrusiveValidation').options);
} else {
newFormjQuery.validate();
}
}
// Reset already ran
alreadyRan = false;
} else {
// No form, probably submitted properly, what next?
" + (!string.IsNullOrWhiteSpace(AjaxifyFormRedirectAfterSubmission) ? $"window.location = '{AjaxifyFormRedirectAfterSubmission}';" : "") + @"
}
}).catch(function (err) {
// There was an error
alert('" + AjaxifyFormErrorMessage + @"');
});
}
return false;
}
}
AjaxifyForm" + randomID + @"();
</script>");
}
}
}
}