# MVC: Merging Templates in a Controller HttpPost Method

> The MailMerge engine and Web.TextControl combine to merge templates server-side in an MVC application. A JavaScript function saves the document as a Base64 string, posts it to an HttpPost controller method, merges it with a DataSet, and loads the result back into the editor.

- **Author:** Bjoern Meyer
- **Published:** 2016-01-19
- **Modified:** 2026-07-17
- **Description:** The MailMerge engine and Web.TextControl combine to merge templates server-side in an MVC application. A JavaScript function saves the document as a Base64 string, posts it to an HttpPost controller method, merges it with a DataSet, and loads the result back into the editor.
- **2 min read** (319 words)
- **Tags:**
  - ASP.NET
  - GitHub
  - Mail Merge
  - Reporting
- **Web URL:** https://www.textcontrol.com/blog/2016/01/19/mvc-merging-templates-in-a-controller-httppost-method/
- **LLMs URL:** https://www.textcontrol.com/blog/2016/01/19/mvc-merging-templates-in-a-controller-httppost-method/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2016/01/19/mvc-merging-templates-in-a-controller-httppost-method/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.Web.MVC.MailMerge

---

The reporting engine MailMerge is used to merge templates with data. In combination with the HTML5 based MVC editor Web.TextControl, the created template can be merged in the MVC controller and loaded back into the view.

The following *HttpPost* method *Merge* accepts the model *DocumentViewModel* which contains the template encoded as a Base64 string. The data source is loaded into a new *DataSet* object. Finally, the template is loaded into a new instance of the *MailMerge* class and merged with the loaded *DataSet*. The created document is returned to the view.

```
[HttpPost]
public string Merge(DocumentViewModel model)
{
    // convert the template to a byte array
    byte[] document = 
        Convert.FromBase64String(model.BinaryDocument);

    // load the the data source
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("/App_Data/data/sample_db.xml"),
        XmlReadMode.Auto);

    byte[] sDocument;

    // create a new ServerTextControl and MailMerge component
    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        MailMerge mm = new MailMerge();
        mm.TextComponent = tx;

        // load the template and merge
        mm.LoadTemplateFromMemory(document, 
            FileFormat.InternalUnicodeFormat);
        mm.Merge(ds.Tables[0], true);

        // save the document
        mm.SaveDocumentToMemory(out sDocument, 
            TXTextControl.BinaryStreamType.InternalUnicodeFormat, null);
    }

    // encode and return the merged document
    return Convert.ToBase64String(sDocument);
}
```

The contoller method is called from the Javascript function *Merge*. The current document is saved into a Base64 encoded string and is being sent using AJAX to the controller method *Merge*.

When the controller method returns successfully, the merged document is loaded into the editor.

```
function Merge() {
    // save the document
    TXTextControl.saveDocument(
        TXTextControl.streamType.InternalUnicodeFormat,
        function (e) {

            var serviceURL = "/Home/Merge";

            // call the controller
            $.ajax({
                type: "POST",
                url: serviceURL,
                contentType: 'application/json',
                data: JSON.stringify({
                    BinaryDocument: e.data
                }),
                success: successFunc,
                error: errorFunc
            });

            // the controller sends back the merged document
            // that is loaded into the editor
            function successFunc(data, status) {
                TXTextControl.loadDocument(
                    TXTextControl.streamType.InternalUnicodeFormat, data);
                alert("Document has been merged successfully.");
            }

            function errorFunc() {
                alert('Error');
            }
        });
}
```

Download the sample from GitHub and test it on your own.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [MVC: Adding an Electronic Signature to Documents in Web.TextControl](https://www.textcontrol.com/blog/2016/02/18/mvc-adding-an-electronic-signature-to-documents-in-webtextcontrol/llms.txt)
- [MVC: Loading a Data Source from a View Model](https://www.textcontrol.com/blog/2016/01/29/mvc-loading-a-data-source-from-a-view-model/llms.txt)
- [MailMerge Class Settings Explained](https://www.textcontrol.com/blog/2019/12/05/mailmerge-settings-explained/llms.txt)
- [MailMerge: Conditional Table Cell Colors using Filter Instructions](https://www.textcontrol.com/blog/2019/06/06/mailmerge-conditional-table-cell-colors-using-filter-instructions/llms.txt)
- [MailMerge: Using Filters to Remove Unwanted Rows](https://www.textcontrol.com/blog/2019/06/04/mailmerge-using-filters-to-remove-unwanted-rows/llms.txt)
- [Different Ways to Create Documents using Text Control Products](https://www.textcontrol.com/blog/2017/10/17/ways-to-create-documents-using-text-control-products/llms.txt)
- [ASP.NET MVC: Implementing a Simplistic, Custom Button Bar](https://www.textcontrol.com/blog/2017/03/13/aspnet-mvc-implementing-a-simplistic-custom-button-bar/llms.txt)
- [ASP.NET MVC: Adding Protected Sections to Documents](https://www.textcontrol.com/blog/2017/03/01/aspnet-mvc-adding-protected-sections-to-documents/llms.txt)
- [ASP.NET: Adding Electronic Signatures to Documents](https://www.textcontrol.com/blog/2016/10/12/aspnet-adding-electronic-signatures-to-documents/llms.txt)
- [Creating Dynamic HTML Forms Using ReportingCloud](https://www.textcontrol.com/blog/2016/09/28/creating-dynamic-html-forms-using-reportingcloud/llms.txt)
- [MVC: Loading a DataSet from the ViewBag](https://www.textcontrol.com/blog/2016/01/20/mvc-loading-a-dataset-from-the-viewbag/llms.txt)
- [HTML5: Display and Handle FormCheckBox Fields](https://www.textcontrol.com/blog/2015/09/22/html5-display-and-handle-formcheckbox-fields/llms.txt)
- [Merging Documents with RESTful Web API's](https://www.textcontrol.com/blog/2015/07/31/merging-documents-with-restful-web-apis/llms.txt)
- [MailMerge: Merge Hyperlinks into Merge Fields](https://www.textcontrol.com/blog/2015/05/15/mailmerge-merge-hyperlinks-into-merge-fields/llms.txt)
- [Learning Mail Merge Templates from Basic Fields to Advanced Reporting Structures](https://www.textcontrol.com/blog/2026/08/27/learning-mail-merge-templates-from-basic-fields-to-advanced-reporting-structures/llms.txt)
- [C# Document Generation: A Developer's Guide for .NET](https://www.textcontrol.com/blog/2026/07/08/csharp-document-generation-developer-guide-for-dotnet/llms.txt)
- [Convert SSRS RDL Reports to DOCX and TX Text Control Templates in .NET C#](https://www.textcontrol.com/blog/2026/06/22/convert-ssrs-rdl-reports-to-docx-and-tx-text-control-templates-in-dotnet-csharp/llms.txt)
- [Building a TX Text Control Project with GitHub Actions and the Text Control NuGet Feed](https://www.textcontrol.com/blog/2026/02/09/building-a-tx-text-control-project-with-github-actions-and-the-text-control-nuget-feed/llms.txt)
- [TX Text Control for Blazor: Mail Merge Integration Tutorial](https://www.textcontrol.com/blog/2025/03/25/tx-text-control-for-blazor-mail-merge-integration-tutorial/llms.txt)
- [Mail Merge: Skipping Records During the Merge Process in .NET C#](https://www.textcontrol.com/blog/2025/02/10/mail-merge-skipping-records-during-the-merge-process-in-net-c-sharp/llms.txt)
- [Mail Merge MS Word DOCX Documents and Convert to PDF in .NET C#](https://www.textcontrol.com/blog/2025/02/07/mail-merge-ms-word-docx-documents-and-convert-to-pdf-in-net-c-sharp/llms.txt)
- [Merging Self-Calculating Business Objects with TX Text Control MailMerge in C#](https://www.textcontrol.com/blog/2024/12/27/merging-self-calculating-business-objects-with-tx-text-control-mailmerge-in-csharp/llms.txt)
- [.NET C# PDF Generation Techniques: Which Method is Right for Your Project?](https://www.textcontrol.com/blog/2024/10/30/net-csharp-pdf-generation-techniques-which-method-is-right-for-your-project/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
