0

I'd use T4 Run-time Text Templates so I can pass data to the html template and render the report simply. But I have an error is there something wrong with my code .

Thanks

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim rpt = New ReportTemplate()
        rpt.Session = New Dictionary(Of String, Object)()
        rpt.Session("Model") = New ReportModel With {
            .CustomerName = "TEST",
            .Date = DateTime.Now,
            .OrderItems = New List(Of OrderItem)() From {
                New OrderItem() With {
                    .Name = "Product 1",
                    .Price = 100,
                    .Count = 2
                },
                New OrderItem() With {
                    .Name = "Product 2",
                    .Price = 200,
                    .Count = 3
                },
                New OrderItem() With {
                    .Name = "Product 3",
                    .Price = 50,
                    .Count = 1
                }
            }
        }
        rpt.Initialize()
        Me.WebBrowser1.DocumentText = rpt.TransformText()
    End Sub
End Class

a model to project

Public Class ReportModel
    Public Property CustomerName() As String
    Public Property [Date]() As DateTime
    Public Property OrderItems() As List(Of OrderItem)
End Class
Public Class OrderItem
    Public Property Name() As String
    Public Property Price() As Integer
    Public Property Count() As Integer
End Class

Add a Run-time Text Template (also known as Preprocessed Text Template) to the project and name it ReportTemplate.tt

<#@ template language="VB" #>
'error below line code Type 'Global.Sample.ReportModel' is not defined
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ parameter name="Model" type="ReportModel"#>
<html>
<head>
    <title></title>
    <style type="text/css">
        body { font-family: Calibri;width:400px;}
        table { text-align:center; }
        .container {width:400px; height:100%;}
    </style>
</head>
<body>
<div class="container">
<h1 style="text-align:center;">Order</h1>
<hr/>
<table style="width:100%">
    <tr>
        <td>Customer: <#=Model.CustomerName#></td>
        <td>Order Date: <#=Model.Date#></td>
    </tr>
</table>
<hr/>
<table style="width:100%">
    <tr><th>Index</th><th>Name</th><th>Price</th><th>Count</th><th>Sum</th></tr>
    <#
        Dim index As Integer =1
    For Each item In Model.OrderItems
 #>
    <tr>
        <td><#=index#></td>
        <td><#=item.Name#></td>
        <td><#=item.Price#></td>
        <td><#=item.Count#></td>
        <td><#=item.Count * item.Price#></td>
    </tr>
    <#
     index += 1
     Next
   Dim total= Model.OrderItems.Sum(Function(x) x.Count * x.Price)
   #>
    <tr><td></td><td></td><td></td><th>Total:</th><th><#=total#></th></tr>
</table>
<div>
</body>
</html>
roy
  • 693
  • 2
  • 11
  • This is a bit of a guess but do you need to import the namespace in which `ReportModel` is declared? – jmcilhinney Aug 28 '23 at 02:25
  • @jmcilhinney , Thank you very much for your reply. Can it be done manually because in the error code line I click the error code there is no option for imports – roy Aug 28 '23 at 02:32

0 Answers0