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

A namespace cannot directly contain members such as fields or methods and 'ReportTemplate' is not defined in T4 Run-time Text Templates vb.net

提问人:roy 提问时间:8/27/2023 更新时间:8/27/2023 访问量:23

问:

我将使用 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>
HTML vb.net Report T4 TextTemplate

评论

0赞 jmcilhinney 8/28/2023
这是一个猜测,但您是否需要导入声明的命名空间?ReportModel
0赞 roy 8/28/2023
@jmcilhinney,非常感谢您的回复。是否可以手动完成,因为在错误代码行中,我单击错误代码,没有导入选项

答: 暂无答案