How to turn a DbSet into a dropdown list, for example?

msurfingworld

New Member
I'm trying to wrap my head around all of this ViewModel stuff and do things the right way. I have several models linked to SQL tables via Entity Framework, like this one:\[code\][Table("HardwareOptions", Schema = "dbo")]public class HardwareOption { [Key] public int RecordID {get; set;} [ForeignKey("Configuration")] public string Configuration {get; set;} public string ComputerManufacturer {get; set;} public string ComputerModel {get; set;} public string ComputerDescription {get; set;} public int MonitorCount {get; set;} public string MonitorManufacturer {get; set;} public string MonitorModel {get; set;} public string MonitorDescription {get; set;}}\[/code\]I have a view model like this:\[code\]public class OrderIndexViewModel { public Customer Customer {get; set;} public MetaUser MetaUser {get; set;} public DbSet<HardwareOption> HardwareOptions {get; set;} public DbSet<Machine> Machines {get; set;} public DbSet<PendingOrder> PendingOrders {get; set;}}\[/code\]And a bit of my controller like this:\[code\]private MyDbContext db = new MyDbContext();OrderIndexViewModel viewmodel = new OrderIndexViewModel();viewmodel.Customer = db.Customers.Find("myselffortesting");viewmodel.MetaUser = db.MetaUsers.Find("myselffortesting");viewmodel.HardwareOptions = db.HardwareOptions;viewmodel.Machines = db.Machines;viewmodel.PendingOrders = db.PendingOrders;return View(viewmodel);\[/code\]So as you can see, in my view, I only need information about one customer/user, but I need to be able to query the entire HardwareOptions, Machines, and PendingOrders tables. Now, if my architecture is entirely wrong, please tell me, because that is more what this question is about. But for something specific, say I want to make a dropdown list from HardwareOptions. Technically, I want each \[code\]SelectItem\[/code\] to say a string combination of the values of several columns, but for now I'll say I just want one, like Configuration. What is the right way to do that? I don't know how to manipulate the \[code\]DbSet\[/code\]. When I tried to make a \[code\]Html.DropDownList\[/code\] from the \[code\]DbSet\[/code\], I got a list with the correct number of items, but they all said "mynamespace.Models.HardwareOption." Which makes sense, I just can't figure out how to do it right.
 
Back
Top