ASP.NET Core 2 .1-如何在表中显示所有数据

发布于 2025-01-30 14:50:43 字数 937 浏览 2 评论 0原文

我不知道如何将customerdata放入客户名单属性中。我已经在使用customerLists = customerdata;,但是我有错误:

“客户名单”是一种类型,但像变量

一样使用

有人可以向我解释这一点吗?

这是我在index.cshtml.cs中的代码:

namespace WebApplication1.Pages
{
    public class Pages : PageModel
    {
        public List<CustomerList> CustomerLists = new List<CustomerList>();

        private readonly ApplicationDbContext _conn;

        public Pages(ApplicationDbContext conn)
        {
            _conn = conn;
        }

        public int Message;

        public void OnGet()
        {
            var CustomerData = _conn.table_customer.ToList();
            //??
        }
    }

    public class CustomerList
    {
        public string CustomerId;
        public string Name;
        public string Address;
        public string MobileNo;
    }
}

I don't know how to put CustomerData into CustomerLists property. I'm already using CustomerLists = CustomerData; but I got error:

'CustomerLists' is a type but is used like a variable

Can anyone explain this to me?

This is my code in Index.cshtml.cs:

namespace WebApplication1.Pages
{
    public class Pages : PageModel
    {
        public List<CustomerList> CustomerLists = new List<CustomerList>();

        private readonly ApplicationDbContext _conn;

        public Pages(ApplicationDbContext conn)
        {
            _conn = conn;
        }

        public int Message;

        public void OnGet()
        {
            var CustomerData = _conn.table_customer.ToList();
            //??
        }
    }

    public class CustomerList
    {
        public string CustomerId;
        public string Name;
        public string Address;
        public string MobileNo;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-02-06 14:50:43

如前所述,您将customerdata分配给customerList code> customerList是一种类型。这是不正确的。

您需要:

CustomerLists = CustomerData;

但是,仅当customerdatalist&lt; customerList&gt;类型时。

对于施法customerdata to list&lt; customerList&gt;键入类型,使用linq .select()

using System.Linq;

public void OnGet()
{
    var CustomerData = _conn.tabel_customer.ToList();
    CustomerLists = CustomerData
        .Select(x => new CustomerList
        {
            // Assign property value
            CustomerId = x.CustomerId,
            ... // Remaining properties
        })
        .ToList();
}

或者,您可以直接分配客户名单值,而无需customerdata变量。

using System.Linq;

public void OnGet()
{
    CustomerLists = _conn.tabel_customer
        .Select(x => new CustomerList
        {
            // Assign property value
            CustomerId = x.CustomerId,
            ... // Remaining properties
        })
        .ToList();
}

通过提供getter和设定器,将字段更改为属性。

public class CustomerList
{
    public string CustomerId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string MobileNo { get; set; }
}

As the error mentioned, you are assigning CustomerData to CustomerList which CustomerList is a type. It's incorrect.

You need:

CustomerLists = CustomerData;

But only when CustomerData is a List<CustomerList> type.

For casting CustomerData to List<CustomerList> type, work with Linq .Select().

using System.Linq;

public void OnGet()
{
    var CustomerData = _conn.tabel_customer.ToList();
    CustomerLists = CustomerData
        .Select(x => new CustomerList
        {
            // Assign property value
            CustomerId = x.CustomerId,
            ... // Remaining properties
        })
        .ToList();
}

Or you can straightway assign the CustomerLists value without the need for CustomerData variable.

using System.Linq;

public void OnGet()
{
    CustomerLists = _conn.tabel_customer
        .Select(x => new CustomerList
        {
            // Assign property value
            CustomerId = x.CustomerId,
            ... // Remaining properties
        })
        .ToList();
}

Change the fields to properties by providing getter and setter.

public class CustomerList
{
    public string CustomerId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string MobileNo { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文