Skip to main content

Autosave drafts in an ASP.NET Core rich text editor

The editor with a document in progress

A browser crash, an expired session or an accidental click can throw away an hour of writing. Autosave keeps a draft in the user's browser and brings it back when they return. In RichTextBox it is one attribute.

Turn it on

<richtextbox name="Body"
             toolbar="default"
             height="400px"
             auto-save="true"></richtextbox>

Type something and reload the page: the text comes back.

Set the delay and the key

<richtextbox name="Body"
             auto-save="true"
             auto-save-delay="2000"
             auto-save-key="[email protected]@User.Identity?.Name"></richtextbox>

Set a key per document. If every article's edit page shares a key, the draft from one article could reappear in another. Including the record's id prevents that. Including the user keeps drafts apart on a shared computer.

Where the draft lives

Drafts are kept in the browser's localStorage. That means:

So autosave protects against lost work. It is not saving. For drafts that follow a user around, also save in the background to your database.

Several editors on one page

Give each its own key, so their drafts do not collide:

<richtextbox name="Summary" auto-save="true" auto-save-key="draft-summary"></richtextbox>
<richtextbox name="Body" auto-save="true" auto-save-key="draft-body"></richtextbox>

Try it in the multiple editor drafts demo.

Read next