Example of Auto Save in ASP.NET MVC Rich Text Editor Control
This sample demonstrates how to save the Rich Text Editor's content automatically with periodic interval. When you type or edit the content, it will be saved automatically every 5 seconds.
Properties
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "autoSaveEditor",
Placeholder = "Start to type a content to save",
SaveInterval = 5000,
EnablePersistence = true,
OnChange = "updateStatus",
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "autoSaveEditor",
Placeholder = "Start to type a content to save",
SaveInterval = 5000,
EnablePersistence = true,
OnChange = "updateStatus",
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
Value = (string)ViewData["RTEValue"]
})
<div id="statusEle" class="current-status" role="status" aria-live="polite">
<div id="saving" style="display: none"><span class="saving-icon" aria-hidden="true"></span><span class="status-text">Saving changes</span></div>
<div id="saved" style="display: none"><span aria-hidden="true">✓</span> <span class="status-text">Changes saved</span></div>
</div>
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="checked">Auto Save</label><input id="checked" type="checkbox" checked /></div>
</div>
<style>
.current-status { min-height: 28px; margin-top: 8px; color: #555; }
.status-text { margin-left: 6px; font-size: 13px; }
.saving-icon { display: inline-block; width: 12px; height: 12px; border: 2px solid #999; border-top-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; vertical-align: -2px; }
@@keyframes spin { to { transform: rotate(360deg); } }
</style>
<script>
// Change is raised after the interval: the status says "saving", then "saved". A page would post the text to its server here.
function updateStatus() {
document.getElementById('saving').style.display = 'block';
document.getElementById('saved').style.display = 'none';
window.setTimeout(function () {
document.getElementById('saving').style.display = 'none';
document.getElementById('saved').style.display = 'block';
}, 500);
}
document.getElementById('checked').addEventListener('change', function (e) {
window.Lumen.get('autoSaveEditor').setSettings({ saveInterval: e.target.checked ? 5000 : 0 });
if (!e.target.checked) {
window.setTimeout(function () {
document.getElementById('saving').style.display = 'none';
document.getElementById('saved').style.display = 'none';
}, 500);
}
});
</script>
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/AutoSave.cshtml; it draws what ViewData holds.
public ActionResult AutoSave()
{
ViewData["Tools"] = new List<string> { "Bold", "Italic", "Underline", "|", "Formats", "Alignments", "Blockquote", "OrderedList", "UnorderedList", "|", "CreateLink", "Image", "|", "SourceCode", "Undo", "Redo" };
ViewData["RTEValue"] = "<h2>Welcome to the Rich Text Editor Demo!📝</h2><p>Experience the power of modern content editing with advanced formatting, media embedding, and many other features. You can explore this demo for yourself.</p>" +
"<h5>Explore the Possibilities! 🚀</h5><ul><li><b>Highly customizable</b> - You can configure the toolbar, enable/disable features, and fine-tune the editing experience to match your needs.</li>" +
"<li><b>Seamless content pasting</b> - Copy and paste from Microsoft Word, Outlook, or other editors or sources while preserving formatting, styles, and structure.</li>" +
"<li><b>Import Word documents</b> - Convert <b>DOCX</b> files into editable HTML content inside the editor using the Import from Word feature.</li>" +
"<li><b>One-click Export</b> - Save your document as <b>PDF</b> 📄 or <b>Word (DOCX)</b> 📝 with just a single click.</li></ul><p><br></p>" +
"<h5>Powerful Features!</h5><p>A quick overview of the essential features of the Rich Text Editor.</p>" +
"<table><thead><tr><th>Feature</th><th>Description</th></tr></thead><tbody><tr><td>Text Formatting</td><td>Bold, Italic, Underline, Strikethrough, and more.</td></tr>" +
"<tr><td>Lists & Indentation</td><td>Ordered, unordered, nested lists.</td></tr><tr><td>Tables</td><td>Insert and edit tables with styling.</td></tr>" +
"<tr><td>Media Embedding</td><td>Images, videos, and audio.</td></tr></tbody></table><p><br></p><p align=\"center\"><b>\"Great writing begins with a great editor.\"</b> ✍</p>";
return View();
}
/// <summary>The Image sample's uploads: kept in memory (the 50 newest), as a demo needs no storage of its own.</summary>
private static readonly ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>> Images = new ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>>();
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The Rich Text Editor provides options to save its content automatically using the SaveInterval property. By default, the save interval time is 10 seconds from
built-in support, but it can be customized as per the application needs. The interval is calculated based on editing the content and is not considered on an idle state.
We have configured the save interval as 5 seconds in this example. You can save the content on the server also using this ‘auto save’ option.
When you disable the ‘Auto Save’ option in the sample, the value will be saved on focus-out from the editor.
EnablePersistence keeps the text in the browser each time it is saved, so the editor starts from it when the page is opened again (it is made safe when it is read back).