[英]How can I add authentication on my project using an already exist user table in ASP.net core?
[英]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;
但僅當CustomerData
是List<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.