代码之家  ›  专栏  ›  技术社区  ›  Kan_IT

命名空间不能直接包含字段或方法等成员,并且T4运行时文本模板vb.net中未定义“ReportTemplate”

  •  0
  • Kan_IT  · 技术社区  · 1 年前

    我会使用T4运行时文本模板,这样我就可以将数据传递到html模板并简单地呈现报告。 但我有一个错误,是不是我的代码出了问题。

    谢谢

    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
    

    要投影的模型

    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
    

    将运行时文本模板(也称为预处理文本模板)添加到项目中,并将其命名为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>
    
    0 回复  |  直到 1 年前