How do I create a ViewModel structure for this hierarchical data that I need to display in ASP.NET MVC?

I have a view that will look like this:

alt text

I am trying to understand how I should present my ViewModel for this view. Each "Agency" can have several "Business units", and each "Business unit" can have several "Clients".

In a database, I easily imagine this with a mapping table and foreign keys for agency tables, BusinessUnit and Client.

However, now I need to use LINQ to query this data from the database, and then build a ViewModel object that represents this tree structure so that my view can render it.

Can someone give me advice on what kind of data structure should I use, or how my ViewModel might look for this hierarchical structure in C # code? I need to create a ViewModel to navigate to this view.

Any suggestions on how to introduce ViewModel are welcome!

+4
source share
3 answers

Just save a List instance in your view data?

public class Agency { public List<BusinessUnit> Units; public string Name; public int NumberOfAccounts { get { int ret = 0; foreach(BusinessUnit unit in units) ret += unit.NumberOfAccounts; return ret; } } // ... continue this strategy for other properties } public class BusinessUnit { public List<Client> clients; public string Name; public int NumberOfAccounts { get { int ret = 0; foreach(Client client in clients) ret += client.NumberOfAccounts; return ret; } } // ... continue this strategy for other properties } public class Client { public string Name; public int NumberOfAccounts; } 
+1
source

I recently used the ADO.Net Entity Data Model data model template to manage communications with an MSSQL database that supports hierarchy data, and it works well.

You can simply snap your presentation level directly to data models.

If your keys are installed correctly in the database, this will be a snap-in to run and run. I think this also requires ADO.Net 3.5

Creating an Entity Framework ADO.NET Object

MS information for the object

0
source

Assuming your Linq2Sql implementation has the same relationships in it as the database (which, if you drag it into the constructor, they definitely do), that’s how I could proceed.

I would create a strongly typed partial view of type Agent that will represent each section (Agency, in your case), name it AgencyReportSection.ascx. This control will be taken by the agency, iterating through its business units, which, in turn, are repeated through their customers.

Wherever you collect your data, do the following:

 DataContext context = new DataContext(); DataLoadOptions options = new DataLoadOptions(); options.LoadWith<Agency>(a => a.BusinessUnit); options.LoadWith<BusinessUnit>(b => b.Client); context.LoadOptions = options; 

What this will give you is that when the agency receives the context, it will follow certain relationships and give you these objects. So you get:

 Agency a = context.Agency.FirstOrDefault(); IEnumerable<BusinessUnit> units = a.BusinessUnits; IEnumerable<Client> clients = units.Clients; 

Your view might do something like:

 <% foreach(var agency in agencies)%{> <% Html.RenderPartial("AgencyReportSection"); %> <%}%> 

The reason you perform the function of loading data is to avoid lazy loading in the view so that the model collects all the necessary data.

I hope I understood your question correctly ...

0
source

Source: https://habr.com/ru/post/1286476/


All Articles