c# - Use of foreign key relationships in a Viewmodel -
trying understand use of foreign key relationships in viewmodels, struggling anywhere it. have 3 tables, tblinventory, tbllocations , tblequipment.
inventory has locationid, foreign key tbllocation , equipmentid, links tblequipment.
the foreign key locationid should display property of "location name", whilst equipmentid should display "equipment type".
for single table, can controller enough, however, can id numbers each property when trying through foreign keys.
the following inventory view model:
public class inventoryindexviewmodel { public int inventoryid { get; set; } public string stationreference { get; set; } public int equipmentid { get; set; } public long locationid { get; set; } public list<tbllocation> locations { get; set; } public list<tblequipment> equipment { get; set; } } the location viewmodel:
public class locationviewmodel { public int locationid { get; set; } public string location { get; set; } } and equipment viewmodel:
public class equipmentviewmodel { public int equipmentid { get; set; } public string equipment { get; set; } } and controller:
public actionresult index() { var theinventory = inventorycontext.tblinventories.tolist(); list<inventoryindexviewmodel> viewmodellist = theinventory .select(u => new inventoryindexviewmodel { inventoryid = u.inventoryid, stationreference = u.stationreference, locationid = u.tbllocation.locationid, equipmentid = u.tblequipment.equipmentid }).tolist(); return view(viewmodellist); } i'm sure i'm doing either miles off, or close not quite there. way i'm connecting viewmodels? or controller off point?
thanks
Comments
Post a Comment