Example of Online Html Editor in ASP.NET MVC Rich Text Editor Control
This sample demonstrates an HTML content LIVE editing scenario using Rich Text Editor.
Script, styles and event handlers are removed when the editor takes this HTML.
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "onlineEditor",
Name = "Online",
Height = "380px",
SaveInterval = 300,
OnCreated = "RteOnline.toSource",
OnChange = "RteOnline.toSource",
ToolbarSettings = new UiRichTextToolbar
{
Type = UiRichTextToolbarType.MultiRow,
Items = (List<string>)ViewData["Tools"]
},
Value = (string)ViewData["RTEValue"]
})
<div class="rte-split">
<div>
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "onlineEditor",
Name = "Online",
Height = "380px",
SaveInterval = 300,
OnCreated = "RteOnline.toSource",
OnChange = "RteOnline.toSource",
ToolbarSettings = new UiRichTextToolbar
{
Type = UiRichTextToolbarType.MultiRow,
Items = (List<string>)ViewData["Tools"]
},
Value = (string)ViewData["RTEValue"]
})
</div>
<div class="rte-code">
<label for="rteOnlineSource" class="small">HTML</label>
<textarea id="rteOnlineSource" spellcheck="false" aria-describedby="rteOnlineNote"></textarea>
<p class="small text-muted mb-0" id="rteOnlineNote">Script, styles and event handlers are removed when the editor takes this HTML.</p>
</div>
</div>
<style>
.rte-split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.rte-code { display: flex; flex-direction: column; }
.rte-code textarea { flex: 1 1 auto; min-height: 320px; font-family: Consolas, "Courier New", monospace; font-size: 13px; padding: 8px; border: 1px solid var(--ui-border, #ccc); border-radius: 4px; }
@@media (max-width: 800px) { .rte-split { grid-template-columns: 1fr; } }
</style>
<script>
window.RteOnline = (function () {
'use strict';
var timer = 0;
/** OnCreated and OnChange: the editor's HTML on the right. */
function toSource() {
var source = document.getElementById('rteOnlineSource');
if (source && document.activeElement !== source) { source.value = window.Lumen.get('onlineEditor').getHtml(); }
}
// The right side's HTML into the editor, once the typing pauses (the editor makes it safe).
document.addEventListener('input', function (event) {
if (event.target.id !== 'rteOnlineSource') { return; }
clearTimeout(timer);
timer = setTimeout(function () { window.Lumen.get('onlineEditor').value = event.target.value; }, 300);
});
return { toSource: toSource };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/OnlineHtmlEditor.cshtml; it draws what ViewData holds.
public ActionResult OnlineHtmlEditor()
{
ViewData["RTEValue"] = "<h2>Hello</h2><p>Change me, or change my HTML on the right.</p>";
ViewData["Tools"] = new List<string> { "Bold", "Italic", "Underline", "|", "Formats", "Alignments", "|", "OrderedList", "UnorderedList", "|", "CreateLink", "CreateTable", "|", "Undo", "Redo" };
return View();
}
}
}
@* 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>
This sample Rich Text Editor enabled most of the features to edit the content quickly such as insert image, table, etc... The source code view is formatted using the Code-mirror plugin.