I’ve made a CRUD MVC app which has something called Parts that are in certain relationships with each other.
PartMain - o:m Section, o:m Report
PartSection - m:o Main, o:m Property
PartProperty - m:o Section, Contains MainID for sorting purposes, SortByMainID
PartReport - m:o Main
All models are working and everything is ok on that part. Now I wish to generate a View that will show all data related to lets say selected PartMain. Like so:
PartMain: Name, Desc, etc... rest of data
PartSection1: data...
PartProperty1: data.. (all properties in relationship with that section)
PartSection2: data... (all sections in relationship with selected PartMain)
PartProperty1: data.. (all properties in relationship with that section)
PartReport: data.... (all reports with selected PartMain)
I know I should do something like foreach loops in View but I’m having trouble grasping the logic, can anyone help?
Thanks.
PartMain contains Name and Desc properties, also is in o:m PartSection, and o:m PartReport. PartSection contains Name and Desc properties, also is in o:m PartProperty. PartProperty contains Name and Desc. PartReport contains Name and Version.
So what I want to do would be with pseudo code something like.
I tried this in controller, passed data with viewbag but nothing came back
pMainID = selectedMainID;
PartMain partMain = db.PartMain.Find(pMainID);
ViewBag.Name = partMain.Name
ViewBag.Desc = partMain.Desc
var partSections = db.PartSection.Where(s => s.pMainID = pMainID);
foreach (var partSec in partSections)
{
ViewBag.PartSecName = partSec.Name
ViewBag.PartSecDesc = partSec.Desc
var partProperties = db.PartProperties.Where(p => p.pSecID = partSec.ID);
foreach(var partProp in partProperties)
{
ViewBag.PartPropName = partProp.Name
ViewBag.PartPropDesc = partProp.Desc
}
}
Now that should output something like I mentioned above. Of course this code won’t work but that should be the general idea. That’s why I ask for help. Thank you.
Controller:
View: