如何将EF Core/Linq查询转换为列表< selectList>?

发布于 2025-02-10 05:26:00 字数 446 浏览 1 评论 0原文

我有以下类别模型:

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 技术交流群。

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

发布评论

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

评论(3

独闯女儿国 2025-02-17 05:26:01

修复DTO列出,也许您也需要添加仓库???

public class CategoryDTO
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int WarehouseId { get; set; } //????
    public List<SelectListItem> WarehouseNames { get; set; }
}

在您的行动中应该是这样的

var model= new CategoryDTO();

var wareHouseNames = context.Set<Warehouse>()
                    .Select ( i=>  new SelectListItem {
                     Text = i.Name,
                     Value = i.Id
                    }).ToList();
model.WareHouseNames = wareHouseNames ;

return View(model)

,并查看

@model CategoryDTO


<select class="form-control"  asp-for="@WarehouseId" asp-items="@WareHouseNames">
         <option value="0" >Select</option>
</select>

fix Dto to List, and maybe you need to add a WarehouseId too???

public class CategoryDTO
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int WarehouseId { get; set; } //????
    public List<SelectListItem> WarehouseNames { get; set; }
}

in you action should be something like this

var model= new CategoryDTO();

var wareHouseNames = context.Set<Warehouse>()
                    .Select ( i=>  new SelectListItem {
                     Text = i.Name,
                     Value = i.Id
                    }).ToList();
model.WareHouseNames = wareHouseNames ;

return View(model)

and view

@model CategoryDTO


<select class="form-control"  asp-for="@WarehouseId" asp-items="@WareHouseNames">
         <option value="0" >Select</option>
</select>
jJeQQOZ5 2025-02-17 05:26:01

您可以将“公开列表”变成字典中,并将键作为ID和值作为“仓库”对象的名称。

You can make the "public List" into Dictionary and use the key as Id and value as Name from "WareHouse" object.

是你 2025-02-17 05:26:01

我认为您要从categorydto完全键入warehousenames属性,而不是填充一个categorydto对象 object appulate a code> list&lt; categorydto&gt;或(甚至更好!)iEnumerable&lt; categorydto&gt;

然后,您将能够将这些类别映射到下拉列表/html select entity。但是,这是什么样子取决于您的问题中的信息。

I think you want to remove the WarehouseNames property from the CategoryDTO type completely, and instead of populating one CategoryDTO object populate a List<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.

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