C# 我的客户列表已填充,但不会显示在服务器端Blazor的Foreach循环中

C# 我的客户列表已填充,但不会显示在服务器端Blazor的Foreach循环中,c#,.net,list,foreach,blazor,C#,.net,List,Foreach,Blazor,我正在学习Blazor。我用CustomerID和CustomerName创建了一个Customer类,我可以将测试客户添加到我的客户列表中,但是当我在Foreach循环中使用该列表(客户)来显示他们时,我得到一个错误“客户的名称不出现在当前上下文中”。请查看代码。我做错了什么 enter code here @page "/displaycustomers" @using System.Collections.Generic @using CustomerBlazorServerA

我正在学习Blazor。我用CustomerID和CustomerName创建了一个Customer类,我可以将测试客户添加到我的客户列表中,但是当我在Foreach循环中使用该列表(客户)来显示他们时,我得到一个错误“客户的名称不出现在当前上下文中”。请查看代码。我做错了什么

enter code here



@page "/displaycustomers"


@using System.Collections.Generic

@using CustomerBlazorServerApp.Data

<h3>Display Customers</h3>

<table class="table">
    <thead>
        <tr>
            <th>CustomerID</th>
            <th>Customer Name</th>

        </tr>
    </thead>
    <tbody>


        @foreach (var acustomer in thecustomers)
            {
            <tr>
                <td>acustomer.CustomerID</td>
                <td>acustomer.CustomerName</td>

            </tr>
            }
    </tbody>
</table>



@code {

  //  protected virtual void OnInitialized ();
    protected override void OnInitialized()
        {

        List<Customer2> thecustomers = new List<Customer2>();

        new Customer2 { CustomerID = "123", CustomerName = "Any Company" };
        new Customer2 { CustomerID = "456", CustomerName = "Some Company" };

        thecustomers.Add(new Customer2 { CustomerID = "123", CustomerName = "Any Company" });
        thecustomers.Add(new Customer2 { CustomerID = "456", CustomerName = "Some Company" });


        }


}
在此处输入代码
@页面“/displaycustomers”
@使用System.Collections.Generic
@使用CustomerBlazorServerApp.Data
展示客户
客户编号
客户名称
@foreach(客户中的var acustomer)
{
客户针
客户名称
}
@代码{
//受保护的虚拟空间初始化();
受保护的覆盖无效OnInitialized()
{
列出客户=新列表();
新客户2{CustomerID=“123”,CustomerName=“任何公司”};
新客户2{CustomerID=“456”,CustomerName=“某公司”};
添加(新客户2{CustomerID=“123”,CustomerName=“任何公司”});
添加(新客户2{CustomerID=“456”,CustomerName=“Some Company”});
}
}
此行:

 List<Customer2> thecustomers = new List<Customer2>();

正如@Kyle所述,将列表移到方法之外。但是您还需要告诉渲染器,
acustomer.CustomerID
是生成的内容

TLDR:
@acustomer.CustomerID

顺便说一句,您不需要使用System.Collections.Generic。默认情况下,它“导入”到razor组件中。还应在_imports.razor.cs文件中定义常用导入

@page "/"
@using System.Collections.Generic

<h3>Display Customers</h3>

<table class="table">
<thead>
    <tr>
        <th>CustomerID</th>
        <th>Customer Name</th>
    </tr>
</thead>
<tbody>
    @foreach (var acustomer in thecustomers)
    {
        <tr>
            <td>acustomer.CustomerID</td>
            <td>acustomer.CustomerName</td>
        </tr>
    }
</tbody>
</table>


@code {
    List<Customer2> thecustomers = new List<Customer2>();

    protected override void OnInitialized()
    {
        thecustomers.Add(new Customer2 { CustomerID = "123", CustomerName = "Any Company" });
        thecustomers.Add(new Customer2 { CustomerID = "456", CustomerName = "Some Company" });
    }

    public class Customer2
    {
        public string CustomerID { get; set; }
        public string CustomerName { get; set; }
    }
}