Products Technologies Demo Docs Blog Support Company

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.

MVC: Loading a Data Source from a View Model

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:

MVC: Loading a data source from a view model

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

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

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.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETReportingGitHub

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…


ASP.NETReportingGitHub

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…


ASP.NETReportingMail Merge

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…


ASP.NETReportingWindows Forms

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…


ASP.NETReportingWindows Forms

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…

Share on this blog post on: