Dynamic Razor Templates And Model Binding Programmatically

DLL Requirements
  1. RazorEngine.dll(3.10.0) -Bind template with model
  2. Rotativa -Converts Razor View to PDF 
Introduction
 
In MVC, we return a View for every action method and pass the Model to the View and bind it to the View. So, we need an action method and a View for every action. That's a lot of action methods, right? Well, we can have only one action method and in that, we can get Razor templates from the database or file and bind the data to the code and pass the result to View. This is possible with the help of RazorEngine.
 
RazorEngine
 
RazorEngine is a templating engine built upon Microsoft's Razor parsing technology. The RazorEngine allows you to use Razor syntax to build robust templates. 
 
Rotativa 
 
Generating a PDF for a report or any document that can be printable in .NET is a bit cumbersome. But this can be achieved in ASP.NET MVC very easily and quickly using Rotativa tools which are available in the NuGet packages. It gives you the flexibility to create PDFs directly from Views or Partial Views or URLs too. 
 
Step 1
 
Get template from the database.
  1. var razorTemplate = "<div class='row'>" +  
  2. "<div class='text-center'>" +  
  3. "<div>@Model.Title</div>" +  
  4. "<div>@Model.Name</div>" +  
  5. "<div>@Model.Age</div>" +  
  6. "</div> </div>";  
Step 2
 
Model to bind.
  1. SampleModel sampleModel = new SampleModel()  
  2. {  
  3.    Title = "Mr",  
  4.    Name = "Raj",  
  5.    Age = 24  
  6. };  
Step 3
 
Bind the Razor template with model.
  1. var razorTemplateString = Engine.Razor.RunCompile(razorTemplate, DateTime.Now.TimeOfDay.ToString(), null, sampleModel);  
Step 4
 
Rotativa returns the View as pdf.
  1. var PDFView = new ViewAsPdf("Print", razorTemplateString)  
  2.    {  
  3.       FileName = "Sample.pdf",  
  4.    };  
  5.    return PDFView;  
  6. }  
Complete Code
  1. public class TemplateController: Controller {  
  2.     public ActionResult Print() {  
  3.         //Get template from database  
  4.         var razorTemplate = "<div class='row'>" + "<div class='text-center'>" + "<div>@Model.Title</div>" + "<div>@Model.Name</div>" + "<div>@Model.Age</div>" + "</div> </div>";  
  5.         //Model to bind  
  6.         SampleModel sampleModel = new SampleModel() {  
  7.             Title = "Mr",  
  8.                 Name = "Raj",  
  9.                 Age = 24  
  10.         };  
  11.         //Bind razor template with model  
  12.         var razorTemplateString = Engine.Razor.RunCompile(razorTemplate, DateTime.Now.TimeOfDay.ToString(), null, sampleModel);  
  13.         //Rotativa to convert view as pdf  
  14.         var PDFView = new ViewAsPdf("Print", razorTemplateString) {  
  15.             FileName = "Sample.pdf",  
  16.         };  
  17.         return PDFView;  
  18.     }  
  19. }  
  20. public class SampleModel {  
  21.     public string Title {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     public string Name {  
  26.         get;  
  27.         set;  
  28.     }  
  29.     public int Age {  
  30.         get;  
  31.         set;  
  32.     }  
  33. }  
Print View
  1. <div style="display: none;">@Model string</div>  
  2. @{  
  3.    Layout = "~/Views/Shared/_PrintLayout.cshtml";  
  4. }  
  5. @Html.Raw(Model)