Example of Insert Special Characters in ASP.NET MVC Rich Text Editor Control
This sample shows how to add your own commands to toolbar of the Rich Text Editor. The “Ω” command is added to insert special characters in the editor. Click the “Ω” command to show the special characters list, and then choose the character to be inserted in the editor.
Special characters
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "specialEditor",
Name = "Special",
Height = "280px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "SpecialCharacter", Text = "Ω", TooltipText = "Insert a special character", OnClick = "RteSpecial.open" }
},
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "specialEditor",
Name = "Special",
Height = "280px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "SpecialCharacter", Text = "Ω", TooltipText = "Insert a special character", OnClick = "RteSpecial.open" }
},
Value = (string)ViewData["RTEValue"]
})
@using (Html.BeginUiDialog(new UiDialogOptions { Id = "specialDialog", Header = "Special characters", Width = "340px", ShowCloseIcon = true, IsModal = true }))
{
<div class="rte-special" role="group" aria-label="Characters">
@foreach (string c in new[] { "©", "®", "™", "€", "£", "¥", "¢", "°", "±", "×", "÷", "≠", "≤", "≥", "∞", "µ", "§", "¶", "†", "‡", "•", "…", "«", "»", "¼", "½", "¾", "→", "←", "✓" })
{
<button type="button" class="btn btn-outline-secondary btn-sm" data-rte-char="@c" aria-label="@c">@c</button>
}
</div>
}
<style>
.rte-special { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
</style>
<script>
window.RteSpecial = (function () {
'use strict';
/** The tool's OnClick: opens the dialog of characters (the editor keeps where the caret was). */
function open() {
window.Lumen.get('specialDialog').show();
}
document.addEventListener('click', function (event) {
var button = event.target.closest('[data-rte-char]');
if (!button) { return; }
window.Lumen.get('specialDialog').hide();
window.Lumen.get('specialEditor').executeCommand('insertText', button.getAttribute('data-rte-char'));
});
return { open: open };
})();
</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/InsertSpecialCharacters.cshtml; it draws what ViewData holds.
public ActionResult InsertSpecialCharacters()
{
ViewData["RTEValue"] = "<p>Prices in euros (€), copyright (©) and degrees (°): put the caret here and use the Ω tool.</p>";
ViewData["Tools"] = new List<string> { "Bold", "Italic", "Underline", "|", "Formats", "|", "SpecialCharacter", "|", "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>
The Rich Text Editor allows you to configure your own commands to its toolbar using toolbarSettings property. The command can be plain text, icon, or HTML template. You can also define the order and group where the command should be included. Bind the action to the command by getting its instance.