Example of Mail Merge in ASP.NET MVC Rich Text Editor Control
This sample demonstrates how to implement a mail merge in the Rich Text Editor sample by inserting placeholders into the editor using custom toolbar item, which are then replaced with actual data to create personalized content.
Insert a field
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "mergeEditor",
Name = "Letter",
Height = "300px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "InsertField", Text = "Insert field", TooltipText = "Insert a merge field", OnClick = "RteMerge.fields" }
},
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "mergeEditor",
Name = "Letter",
Height = "300px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "InsertField", Text = "Insert field", TooltipText = "Insert a merge field", OnClick = "RteMerge.fields" }
},
Value = (string)ViewData["RTEValue"]
})
<div class="rte-actions mt-2">
<label>Customer
<select id="rteMergeCustomer" class="form-select form-select-sm">
<option value="0">Maria Santos</option>
<option value="1">Ken Ito</option>
<option value="2">Ana <Ruiz></option>
</select>
</label>
<button type="button" class="btn btn-primary btn-sm" id="rteMergeRun">Merge</button>
</div>
<div class="rte-merged mt-3" id="rteMerged" aria-live="polite" aria-label="The merged letter"></div>
@using (Html.BeginUiDialog(new UiDialogOptions { Id = "mergeFieldDialog", Header = "Insert a field", Width = "300px", ShowCloseIcon = true, IsModal = true }))
{
<div class="rte-fields" role="group" aria-label="Fields">
@foreach (string f in new[] { "FirstName", "LastName", "OrderId", "ShipDate", "City" })
{
<button type="button" class="btn btn-outline-secondary btn-sm" data-rte-field="@f">@f</button>
}
</div>
}
<style>
.rte-actions { display: flex; align-items: center; gap: 12px; }
.rte-actions label { display: flex; align-items: center; gap: 6px; }
.rte-fields { display: grid; gap: 6px; }
.rte-merged { padding: 12px; border: 1px dashed var(--ui-border, #ccc); border-radius: 4px; min-height: 60px; }
</style>
<script>
window.RteMerge = (function () {
'use strict';
var customers = [
{ FirstName: 'Maria', LastName: 'Santos', OrderId: 'SO-10482', ShipDate: '3 November', City: 'Lisbon' },
{ FirstName: 'Ken', LastName: 'Ito', OrderId: 'SO-10483', ShipDate: '4 November', City: 'Osaka' },
// A value with markup: it is shown as text, never as HTML.
{ FirstName: 'Ana', LastName: '<b onclick="alert(1)">Ruiz</b>', OrderId: 'SO-10484', ShipDate: '5 November', City: 'Seville' }
];
function escape(text) {
return String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
/** The Insert field tool's OnClick: the list of fields (the editor keeps where the caret was). */
function fields() {
window.Lumen.get('mergeFieldDialog').show();
}
document.addEventListener('click', function (event) {
var field = event.target.closest('[data-rte-field]');
if (field) {
window.Lumen.get('mergeFieldDialog').hide();
window.Lumen.get('mergeEditor').executeCommand('insertText', '{{' + field.getAttribute('data-rte-field') + '}}');
return;
}
if (event.target.id !== 'rteMergeRun') { return; }
var customer = customers[Number(document.getElementById('rteMergeCustomer').value)];
// The letter is the editor's safe HTML; each field becomes the customer's value as text.
var html = window.Lumen.get('mergeEditor').getHtml().replace(/\{\{(\w+)\}\}/g, function (all, name) {
return Object.prototype.hasOwnProperty.call(customer, name) ? escape(customer[name]) : all;
});
document.getElementById('rteMerged').innerHTML = html;
});
return { fields: fields };
})();
</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/MailMerge.cshtml; it draws what ViewData holds.
public ActionResult MailMerge()
{
ViewData["RTEValue"] = "<p>Dear {{FirstName}} {{LastName}},</p><p>Your order <b>{{OrderId}}</b> ships on {{ShipDate}} to {{City}}.</p><p>Kind regards,<br>The shop</p>";
ViewData["Tools"] = new List<string> { "Bold", "Italic", "Underline", "|", "Formats", "|", "InsertField", "|", "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 mail merge in the Rich Text Editor sample enables users to insert placeholders for personalized content. These placeholders are replaced with actual data when generating the final content, making it easy to create customized letters, invoices, and more.
The following configurations are used in this sample:
- The
ButtonandDropDownButtonare configured in the custom toolbar of the Rich Text Editor. - The
Buttonclick action performs the merge of the editor placeholder content. - The
DropDownButtonprovides a list of available fields, such as "First Name" or "Email Address." A selected field from this dropdown is inserted into the editor as a placeholder. - The
Mentionallows insertion of merge fields by pressing the mention character, such as{, in the editor and selecting an item. These chips make it easy to see and select fields directly within the content.