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?
3
Answers
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.fix Dto to List, and maybe you need to add a WarehouseId too???
in you action should be something like this
and view
You can make the "public List" into Dictionary and use the key as Id and value as Name from "WareHouse" object.