ASP.NET Core MVC: Periodic submission of form - Stack Overflow

admin2025-04-28  2

I would like to submit my web form at least periodically while the user is editing it to avoid losing any data in case of any unexpected issues (while keeping the form open to allow for further user interaction).

Ideally though I would like to submit the form and update the records as soon as, for example, a checkbox was checked and update the records. Is that possible without any JS, Ajax or possible at all?

When just submitting the form itself by ASP.NET methods further edits to the form are no longer possible.

I would like to submit my web form at least periodically while the user is editing it to avoid losing any data in case of any unexpected issues (while keeping the form open to allow for further user interaction).

Ideally though I would like to submit the form and update the records as soon as, for example, a checkbox was checked and update the records. Is that possible without any JS, Ajax or possible at all?

When just submitting the form itself by ASP.NET methods further edits to the form are no longer possible.

Share Improve this question edited Jan 8 at 21:01 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 8 at 13:34 user01012024user01012024 11 2
  • 1 "Is that possible without any JS, Ajax" - No. If you're looking to perform client-side logic, JavaScript is the tool you're looking for. If you're looking to communicate with the server in the background without page reloads, AJAX is the tool you're looking for. (There may exist tools/frameworks which have written this code for you so you don't need to, but those tools would internally rely on JavaScript/AJAX.) – David Commented Jan 8 at 13:54
  • What you describe is basically the functionality of an interactive Single Page Application in a static page. Choose, you can't have both. – MrC aka Shaun Curtis Commented Jan 8 at 15:33
Add a comment  | 

1 Answer 1

Reset to default 0

Apart from the strictly technical problems pointed out in the comments, this is also difficult for other reasons.

Let's assume, for the sake of argument, that you implement the desired periodic update process using either JavaScript or WebAssembly. (The latter option will formally circumvent the requirement that you use no JavaScript or AJAX, but the difference is at best semantic.)

Most well-designed web forms will typically have various validation features that help the user fill out the form. Some validation rules may state a relationship between various parts of the form.

As an example, consider booking a hotel room. When filling out such a form, you'll have to pick arrival and departure dates. A validation rule should state that the arrival date must be before the departure date. The form, in its entirety, is invalid until this rule is fulfilled, and that requires that both arrival and departure dates are filled out.

If you want to periodically save the state of the form, it's likely that the form will be in an invalid state. In the hotel-booking example, this could happen if the user has picked an arrival date, but not yet a departure date.

Thus, if you want to periodically save the state of the form, you'll need to figure out how to submit and handle incomplete data.

Often, server-side code that handles incoming form data will only save the data if the input is valid.

If you want to periodically save state, you'll either need to write explicit server-side code that handles invalid form data. Do you want to save invalid data in the same data store as the database that holds valid data?

Or do you rather want to save 'state snapshop' data in specialized session storage?

None of this is impossible, but it may involve more work than most people realize.

Often, if you have a complex form, it's better to break it up into multiple steps (Wizard), and use an explicit server-side state machine to keep track of the user's progress.

This also addresses another concern: With a wizard, the user will explicitly give consent to move forward. If, on the other hand, you use some background client-side code to periodically 'save the state of the form', you may post something to the server that the user would rather that you didn't. Are you ready to take on that responsibility?

转载请注明原文地址:http://anycun.com/QandA/1745854101a91258.html