如何将EF Core/Linq查询转换为列表< selectList>?
我有以下类别模型:
public class CategoryDTO
{
public int Id { get; set; }
public string Name { get; set; }
public List<SelectList> WarehouseNames { get; set; }
}
仓库模型:
public class Warehouse
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public int MaxCapacity { get; set; }
}
我想在数据库中获取所有仓库和仓库。下拉列表。有人知道我该怎么做吗?
I have the following Category Model:
public class CategoryDTO
{
public int Id { get; set; }
public string Name { get; set; }
public List<SelectList> WarehouseNames { get; set; }
}
Warehouse Model:
public class Warehouse
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public int MaxCapacity { get; set; }
}
I want to get all the Warehouse.Id and Warehouse.Name in my database and store them in the CategoryDTO.WarehouseNames (value: Id, Text: Name) so I can display them in a dropdown list. Does anybody know how can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修复DTO列出,也许您也需要添加仓库???
在您的行动中应该是这样的
,并查看
fix Dto to List, and maybe you need to add a WarehouseId too???
in you action should be something like this
and view
您可以将“公开列表”变成字典中,并将键作为ID和值作为“仓库”对象的名称。
You can make the "public List" into Dictionary and use the key as Id and value as Name from "WareHouse" object.
我认为您要从
categorydto
完全键入warehousenames
属性,而不是填充一个categorydto
对象 object appulate acode> list&lt; categorydto&gt;
或(甚至更好!)iEnumerable&lt; categorydto&gt;
。然后,您将能够将这些类别映射到下拉列表/html
select
entity。但是,这是什么样子取决于您的问题中的信息。I think you want to remove the
WarehouseNames
property from theCategoryDTO
type completely, and instead of populating oneCategoryDTO
object populate aList<CategoryDTO>
or (even better!)IEnumerable<CategoryDTO>
.Then you will be able to map these CategoryDTO items to a dropdown list/HTML
Select
entity. Exactly what that looks like, though, depends on information not yet in your question.