MVC: Loading a Data Source from a View Model
When merging templates with business objects, the Reports ribbon drop-downs in Web.TextControl can be populated from a serialized view model. The controller serializes a class model with the Serializable attribute into XML, stores it in the ViewBag, and the view uses LoadXMLDatabase.

The most typical application for the Web.TextControl is the creation of templates for the Text Control Reporting engine DocumentServer.MailMerge. The ribbon tab Reports is designed to insert merge fields and merge blocks compatible to the reporting classes of TX Text Control.
The drop-down buttons Select Master Table, Merge Field and Merge Block are pre-filled with items of a specific Database Excerpt XML file or a DataSet that can be loaded programmatically.
Typically, the template is merged server-side with a business object created from a class model. In this case, it makes sense to pre-fill the drop-down lists from a view model as well. The following simple model is used in this sample:
[Serializable]
public class address
{
public string Name { get; set; }
public string Street { get; set; }
}
Note the Serializable attribute in the class model. In the controller, the model object is created and typically filled with data from a database. After that, the object is serialized and stored in the ViewBag:
public ActionResult Index()
{
// create a new instance of the model
// fill the model from your data source here
address newAddress = new address()
{
Name = "Peter Jackson",
Street = "2332 Paul Jackson Dr"
};
// create a new XML document
XmlDocument xml = SerializeToXmlDocument(newAddress);
// fill the ViewBag and return the view
ViewBag.xmlDataSource = xml;
return View();
}
In the view, the LoadXMLDatabase method loads the stored XML document from the ViewBag:
@Html.TXTextControl().TextControl(settings =>
{
settings.Dock = DockStyle.Window;
}).LoadXMLDatabase(
(XmlDocument)ViewBag.xmlDataSource).Render()
The drop-down lists are now pre-populated with the available merge fields and merge blocks:
Download the sample from GitHub and test it on your own.
![]()
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2012 or better
- TX Text Control .NET Server (trial sufficient)
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
MVC: Adding an Electronic Signature to Documents in Web.TextControl
An MVC sample uses the MailMerge class to populate a signature template with the signer name and image, then inserts it into a named text frame in a contract document. An async controller method…
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…
MailMerge Class Settings Explained
The MailMerge class provides four properties to control merge output: RemoveEmptyFields, RemoveEmptyLines, RemoveEmptyBlocks, and RemoveEmptyImages. Each property handles unmatched or missing data…
MailMerge: Conditional Table Cell Colors using Filter Instructions
The TX Text Control MailMerge FieldMerged event exposes TableCell instances during merge for conditional formatting. A CellFilterInstructions class parses filter rules from merge field names and…
MailMerge: Using Filters to Remove Unwanted Rows
TX Text Control merge block filters remove unwanted rows from repeating data during the mail merge process without modifying the underlying source. Filter conditions applied to merge blocks…
