Im Trying to create a tree view menu, but it isnt working properly. There are 3 different stored procedures, 1st for families (parent), 2nd for categories(child), and 3rd for reports(grandchild). Families SP takes user id as paramters and seems to be working fine. Category SP takes userID and familyID and 3rd takes userID, FamilyID and CategoryID. When runnning the code it does display the families, and displays the 1 report name underneath. If I remove line of code that calls the stored procedure out of the foreach loop, it does return categories for 1 family, although it displays on all the families.
If I add a breakpoint on on the controller it will go through the loops and return the correct data, so I assume the issue is in the way it is being displayed .[Displayed data] On this screenshot is possible to see how the data is being displayed, being EPOS and RES being retuned from family SP and WebBooking which is a category
FAMILIES
¦
¦--CATEGORIES
¦
¦
¦---REPORT_NAME
Thank you In advance!
On the controller I'm calling each of the stored procedures, and if tested with a breakpoint all the data that should be returned is being returned properly. Perhaps a solution for my problem would be populate the view with the returned data from the controller, however I don't know how to do it?
Controller
foreach (var cat in model.familiesReport = context.P_Mob_Get_ReportFamilies(user).ToList())
{
Console.WriteLine(cat.FamilyName);
foreach (var name in model.CategoriesReport = context.P_Mob_Get_ReportCategories(user, cat.FamilyID).ToList())
{
Console.WriteLine(name.Category);
foreach (var test in model.namesReport = context.P_Mob_Get_ReportNames(user, cat.FamilyID, name.CategoryID).ToList())
{
Console.WriteLine(test.ReportName);
}
}
I have got a View Model that has the 3 SP_Results (Complex types in EF). On the view I am going through every single Complex type and return the name for each one, (Family, Category, Name)
View
<ul>
@foreach (var family in Model.familiesReport)
{
<a href="#" class="menu_fam">@family.FamilyName</a>
<ul class="">
@foreach (var cat in Model.CategoriesReport)
{
<li href="#" class=""><a class="menu_cat"> @cat.Category</a></li>
<ul class="">
@foreach (var name in Model.namesReport)
{
<li href="#" class=""><a class="menu_name"> @name.ReportName</a></li>
}
</ul>
}
</ul>
}
</ul>
CodePudding user response:
I have finally found the solution for this. Ive created a new model and instead of saving the results of the stored procedure in the complex types generated by EF saved them in this model. Each class in the model has a list where the data will be saved. as displayed bellow. Model:
public class FamilyResultResponse
{
public List<Family> Families { get; set; }
public List<Sites> SitesGet { get; set; }
}
public class Family
{
public Nullable<int> FamilyID { get; set; }
public string FamilyName { get; set; }
public List<FamilyResultCat> FamilyCat { get; set; }
}
public class FamilyResultCat
{
public Nullable<int> familyid { get; set; }
public Nullable<int> CategoryID { get; set; }
public string Category { get; set; }
public int Allowed { get; set; }
public List<FamilyResulReportByCategory> FamilCatRep { get; set; }
}
public class FamilyResulReportByCategory
{
public Nullable<int> ReportID { get; set; }
public Nullable<int> categoryid { get; set; }
public string ReportName { get; set; }
}
Controller: Iterate through the lists
public ActionResult Index(Guid? userID\)
{
var model = new FamilyResultResponse();
model.Families=new List<Family>();
SPMenuModel modelSPMEnu = new SPMenuModel();
foreach (var f in context.P_Mob_Get_ReportFamilies(userID).ToList())
{
var fam = new Family();
fam.FamilyID = f.FamilyID;
fam.FamilyName= f.FamilyName;
fam.FamilyCat = new List<FamilyResultCat>();
Console.WriteLine(f.FamilyName);
foreach (var c in context.P_Mob_Get_ReportCategories(userID, f.FamilyID).ToList())
{
var famcat = new FamilyResultCat();
famcat.CategoryID = c.CategoryID;
famcat.Category = c.Category;
famcat.Allowed = c.Allowed;
famcat.FamilCatRep= new List<FamilyResulReportByCategory>();
Console.WriteLine(c.Category);
foreach (var n in context.P_Mob_Get_ReportNames(userID, f.FamilyID, c.CategoryID).ToList())
{
var famcatrep = new FamilyResulReportByCategory();
famcatrep.ReportID = n.ReportID;
famcatrep.ReportName = n.ReportName;
famcatrep.categoryid = n.categoryid;
Console.WriteLine(n.ReportName);
famcat.FamilCatRep.Add(famcatrep);
}
fam.FamilyCat.Add(famcat);
}
model.Families.Add(fam);
}
modelSPMEnu.familyResult = model;
return View(model);
View
@foreach (var fam in Model.Families)
{
<a href="#" class="menu_fam"> @fam.FamilyName</a>
<ul class="">
@foreach (var cat in fam.FamilyCat)
{
<li href="#" class=""><a class="menu_cat"> @cat.Category </a></li>
foreach (var name in cat.FamilCatRep)
{
<ul class="">
<li class=""><a>@name.ReportName</a></li>
</ul>
}
}
</ul>
}