# docx-editor-mail-merge

> Document editor demonstrating mail merge fields populated with recipient data.

**Framework:** aspnet-core  **Component:** DocxEditor  **Variant:** mail-merge

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/aspnet-core/docx-editor-mail-merge.json
```

**Install Package(s)**

```bash
dotnet add package Syncfusion.AspNetCore.DocumentEditor
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/docx-editor-mail-merge/docx-editor.cshtml

```cshtml
@page
@namespace aspnet_core.DocxEditor.MailMerge
@model EJ2CoreSampleBrowser.DocxEditor.MailMerge.MailMergeModel
@using Syncfusion.EJ2

    <div class="control-section">
        <div id='documenteditor_titlebar' class="e-de-ctn-title"></div>
        <div style="display: flex; gap: 12px; padding-top: 8px;">
            <div style="width: 200px; padding: 8px; border: 1px solid #eee;">
                <h6>Insert Field</h6>
                <div id="fieldList">
                    @foreach (var field in Model.Fields)
                    {
                        <div class="merge-field" data-field="@field.Name" title="@field.Category"
                             style="border: 1px solid #ddd; padding: 4px 6px; margin-bottom: 4px; cursor: pointer;">
                            @field.Name
                        </div>
                    }
                </div>
            </div>
            <div style="flex: 1;">
                <ejs-documenteditorcontainer id="container" height="590px" enableToolbar="true"></ejs-documenteditorcontainer>
            </div>
        </div>
    </div>

    <script>
        // Clicking a field inserts a MERGEFIELD into the document at the
        // current selection. The actual mail merge (substituting field values
        // and emitting the final document) is left out of this minimal sample.
        document.addEventListener('DOMContentLoaded', function () {
            var container = document.getElementById('container').ej2_instances[0];
            if (!container) return;
            var list = document.getElementById('fieldList');
            list.addEventListener('click', function (e) {
                var item = e.target.closest('.merge-field');
                if (!item) return;
                var fieldName = item.getAttribute('data-field');
                if (fieldName && container.documentEditor && container.documentEditor.editor) {
                    container.documentEditor.editor.insertField('MERGEFIELD ' + fieldName + ' \\* MERGEFORMAT');
                }
            });
        });
    </script>

```

### src/components/docx-editor-mail-merge/docx-editor.cshtml.cs

```cs
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace EJ2CoreSampleBrowser.DocxEditor.MailMerge
{
    public class MailMergeModel : PageModel
    {
        public List<FieldItem> Fields { get; set; } = new List<FieldItem>();

        public void OnGet()
        {
            Fields = new List<FieldItem>
            {
                new FieldItem { Name = "FirstName", Category = "Recipients" },
                new FieldItem { Name = "LastName",  Category = "Recipients" },
                new FieldItem { Name = "Address",   Category = "Recipients" },
                new FieldItem { Name = "OrderId",   Category = "Orders" },
                new FieldItem { Name = "OrderDate", Category = "Orders" },
            };
        }
    }

    public class FieldItem
    {
        public string Name { get; set; } = string.Empty;
        public string Category { get; set; } = string.Empty;
    }
}

```
