我刚开始学习C#,我的问题可能听起来很愚蠢...
如何使用数据库中的数据填充我的viewmodel中的列表?
My Viewmodels:
public class TransferSpecificClientsViewModel
{
public string TransferFrom { get; set; }
public string TransferTo { get; set; }
public virtual ICollection<ClientsToTransfer> ClientsToTransferList { get; set; }
}
public class ClientsToTransfer
{
public bool IsSelected { get; set; }
public int ClientId { get; set; }
public int UserId { get; set; }
public string Name { get; set; }
}
在控制器中我可能需要这样的东西:
var model = (from p in db.Clients
select new TransferSpecificClientsViewModel()
{
// And here I need to populate the ClientsToTransferList with data from db.Clients... I think it should probably be something like
//ClientsToTransferList.ClientId = p.Id
//ClientsToTransferList.UserId = p.UserId
//ClientsToTransferList.Name = p.Name });
但对于我数据库中的每个客户端
我只是不知道如何让它发挥作用......而且不熟悉C#我不知道如何正确地搜索我想要做的事情......
答案 0 :(得分:1)
你真的很亲近。我将virtual ICollection<ClientsToTransfer>
更改为List<ClientsToTransfer>
并按照您的想法进行选择。
//...initialize TransferFrom and TransferTo properties of your view model
model.ClientsToTransferList =
(from p in db.Clients
select new ClientsToTransfer()
{
ClientId = p.Id
UserId = p.UserId
Name = p.Name
}).ToList();