簡體   English   中英

我如何使用 ASP.NET Core 2 .1 顯示表中的所有數據為空

[英]How i can show all data in table using ASP.NET Core 2 .1 empty

我不知道如何將var Customer放入CustomerList屬性。 我已經在使用CustomerList = CustomerData; 但我得到了錯誤

'CustomerList' 是一種類型,但用作變量

誰能給我解釋一下?

這是我在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;
    }
}

正如所提到的錯誤,您正在將CustomerData分配給CustomerList ,其中CustomerList是一種類型。 這是不正確的。

你需要:

CustomerLists = CustomerData;

但僅當CustomerDataList<CustomerList>類型時。

要將CustomerData轉換為List<CustomerList>類型,請使用 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()
}

或者,您可以直接分配CustomerLists值,而無需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 和 setter 將字段更改為屬性。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM