Website For Data Reporting With DotVVM Business Pack

Introduction 

 
In many cases when we working with systems that aim to perform operations on a database, there is a need to include a section within these systems to visualize the corresponding data. Within web pages, tables are typically used to represent information, search sections to filter information, and more.
In this article we will learn how to design in a simple way, a web page to visualize certain data hosted in tables in a database relates through ASP.NET Core and DotVVM. In previous articles, we were able to learn in general how to use predefined DotVVM controls for data visualization as a report. Here are some of those articles:
This time we will learn the basics of visualizing certain data and establishing some search criteria with C# and HTML, through DotVVM's premium controls on ASP.NET Core, referred to as DotVVM Business Pack.
 

Model, View, ViewModel Pattern

 
DotVVM is based on the Design Pattern Model, View, ViewModel over .NET for communication between HTML (web pages) and C- (source code). The purpose of these parts are as follows:
  • Model. — is responsible for all application data and related business logic.
  • The view. — Representations for the end-user of the application model. The view is responsible for displaying the data to the user and allowing manipulation of application data.
  • Model-View or View-Model. — one or more per view; the model-view is responsible for implementing view behavior to respond to user actions and for easily exposing model data.

How do we Access DotVVM Business Pack?

 
Business Pack is a private NuGet, in which we can make use of the competent premiums already established by DotVVM for the construction of web applications in the business field.
 
For the installation of the DotVVM Business Pack version, it is necessary to configure a few minutes to be able to use these functionalities. It all comes down to the following:
  • Install the DotVVM extension for Visual Studio 2019.
  • Purchase Business Pack (trial version exists) at the following address: DotVVM Business Pack.
  • Sign in to the DotVVM extension for Visual Studio 2019. To do this, we can go to the Visual Studio options menu in the path: Extensions -> DotVVM -> About. Ready, that'll be it.
Website For Data Reporting With DotVVM Business Pack
 

Report with ASP.NET Core and DotVVM

 
To exemplify the use of some DotVVM Business Pack controls for reporting, we have a small application like this:
 
Website For Data Reporting With DotVVM Business Pack
 
Website For Data Reporting With DotVVM Business Pack
 
Taking into account the MVVM – Model, View, ViewModel pattern, we will analyze in general each of these parts for this project, which aims to display the data of particular users.
 
Model
 
Application and related business logic data is handled in this section. In this sense, we will see how the data and the corresponding services are handled.
 
The database consists of two tables: Person and PersonType.
 
Website For Data Reporting With DotVVM Business Pack
 
The SQL statements for creating these tables, their attributes, and inserting some records are as follows: 
  1. CREATE SCHEMA IF NOT EXISTS `dbperson` DEFAULT CHARACTER SET utf8;    
  2. USE `dbperson` ;    
  3.     
  4. CREATE TABLE IF NOT EXISTS `dbperson`.`PersonType` (    
  5.   `Id` INT NOT NULL,    
  6.   `NameVARCHAR(45) NOT NULL,    
  7.   `Description` VARCHAR(45) NOT NULL,    
  8.   PRIMARY KEY (`Id`))    
  9. ;    
  10.     
  11. CREATE TABLE IF NOT EXISTS `dbperson`.`Person` (    
  12.   `Id` INT NOT NULL AUTO_INCREMENT,    
  13.   `FirstName` VARCHAR(45) NOT NULL,    
  14.   `LastName` VARCHAR(45) NOT NULL,    
  15.   `IdPersonType` INT NOT NULL,    
  16.   PRIMARY KEY (`Id`),    
  17.   FOREIGN KEY (`IdPersonType`) REFERENCES `dbperson`.`PersonType` (`Id`))    
  18. ;    
  19.     
  20. INSERT INTO `persontype` (`Id`, `Name`, `Description`) VALUES ('1''Type A''');    
  21. INSERT INTO `persontype` (`Id`, `Name`, `Description`) VALUES ('2''Type B''');    
  22.     
  23. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('1''Sergey''Brin''1');    
  24. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('2''Larry''Page''1');    
  25. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('3''Tim''Barners''2');    
  26. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('4''Linus''Torvalds''1');    
  27. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('5''Larry''Ellison''1');    
  28. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('6''Steve''Ballmer''2');    
  29. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('7''Steve''Jobs''2');    
  30. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('8''Marc''Benioff''1');    
  31. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('9''Ray''Ozzie''2');    
  32. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('10''Nicholas''Negroponte''2');    
  33. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('11''Diane''Green''1');    
  34. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('12''Sam''Palmisano''1');    
  35. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('13''Blake''Ross''2');    
  36. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('14''Ralph''Szygenda''2');    
  37. INSERT INTO `person` (`Id`, `FirstName`, `LastName`, `IdPersonType`) VALUES ('15''Rick''Dalzell''2');     
With the database set, the portion of the data access layer refers to the definition of the classes to work with the database features and the context to establish communication between ASP.NET Core and the database, which in this case MySQL is the one being used.
 
For this purpose, you need to install three NuGet packages:
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools
  • MySql.Data.EntityFrameworkCore
If you are working with SQL Server, the NuGet package to install will be Microsoft.EntityFrameworkCore.SQLServer.
 
You then need to use the package management console to scaffold from the database (automatically generate the context and feature classes) using the following command:
  1. Scaffold-DbContext "server=servername;port=portnumber;user=username;password=pass;database=databasename" MySql.Data.EntityFrameworkCore -OutputDir Entities -f   
With this first part, the connection to the database is listed. What follows is the definition of models with which the website will be worked. These models are:
  1. public class PersonModel  
  2. {  
  3.     public int Id { get; set; }  
  4.     public string FirstName { get; set; }  
  5.     public string LastName { get; set; }  
  6.     public int IdPersonType { get; set; }  
  7.     public string NamePersonType { get; set; }  
  8. }  
  1. public class PersonTypeModel  
  2. {  
  3.     public int Id { get; set; }  
  4.     public string Name { get; set; }  
  5.     public string Description { get; set; }  
  6. }  
For each of these models, there is a service, which has the following operations:
 
PersonService 
  • GetAllPersonsAsync()
  • GetPersonByIdAsync(int personId)
  • GetPersonByIdAndTypeAsync(int personId, int personTypeId)
  • GetAllPersonsByTypeAsync(int personTypeId)
PersonTypeService
  • GetAllPersonTypesAsync()
  • GetPersonTypeByIdAsync(int personTypeId)
In Visual Studio 2019, we'll have something like this:
 
Website For Data Reporting With DotVVM Business Pack
 
ViewModel
  1. public class DefaultViewModel : MasterPageViewModel  
  2. {  
  3.     private readonly PersonService personService;  
  4.     public BusinessPackDataSet<PersonModel> Persons { get; set; } = new BusinessPackDataSet<PersonModel>();  
  5.   
  6.     public List<int> PersonTypes { get; set; } = new List<int>();  
  7.     public string IdSearch { get; set; }  
  8.     public bool SearchByTextVisible { get; set; } = false;  
  9.     public bool IsWindowDisplayed { get; set; }  
  10.   
  11.     public DefaultViewModel(PersonService personService)  
  12.     {  
  13.         this.personService = personService;  
  14.     }  
  15.   
  16.     public async Task UpdatePersonList()  
  17.     {  
  18.         IdSearch = null;  
  19.   
  20.         if (PersonTypes.Count == 2)  
  21.         {  
  22.             Persons.Items = await personService.GetAllPersonsAsync();  
  23.             SearchByTextVisible = true;  
  24.         }  
  25.         else if (PersonTypes.Count == 1)  
  26.         {  
  27.             int IdPersonType = PersonTypes.FirstOrDefault();  
  28.             Persons.Items = await personService.GetAllPersonsByTypeAsync(IdPersonType);  
  29.             SearchByTextVisible = true;  
  30.         }  
  31.         else  
  32.         {  
  33.             Persons.Items.Clear();  
  34.             SearchByTextVisible = false;  
  35.         }  
  36.     }  
  37.   
  38.     public async Task SearchById()  
  39.     {  
  40.         if (PersonTypes.Count == 2)  
  41.         {  
  42.             if (!string.IsNullOrEmpty(IdSearch))  
  43.             {  
  44.                 List<PersonModel> list = new List<PersonModel>(); ;  
  45.                 list.Add(await personService.GetPersonByIdAsync(Int32.Parse(IdSearch)));  
  46.                 Persons.Items = list;  
  47.             }  
  48.             else  
  49.             {  
  50.                 Persons.Items = await personService.GetAllPersonsAsync();  
  51.             }  
  52.         }  
  53.         else if (PersonTypes.Count == 1)  
  54.         {  
  55.             if (!string.IsNullOrEmpty(IdSearch))  
  56.             {  
  57.                 int IdPersonType = PersonTypes.FirstOrDefault();  
  58.                 List<PersonModel> list = new List<PersonModel>(); ;  
  59.                 list.Add(await personService.GetPersonByIdAndTypeAsync(Int32.Parse(IdSearch), IdPersonType));  
  60.                 Persons.Items = list;  
  61.             }  
  62.             else  
  63.             {  
  64.                 int IdPersonType = PersonTypes.FirstOrDefault();  
  65.                 Persons.Items = await personService.GetAllPersonsByTypeAsync(IdPersonType);  
  66.             }  
  67.         }  
  68.     }  
  69. }  
View
  1. <dot:Content ContentPlaceHolderID="MainContent">  
  2.   
  3.     <div class="content">  
  4.   
  5.         <img src="/Resources/Images/tree.svg" class="content__background-image content__background-image--left" />  
  6.   
  7.         <a href="https://www.dotvvm.com/" target="_blank">  
  8.             <img src="/Resources/Images/text.svg" class="content__image" />  
  9.         </a>  
  10.         <h1>PERSON REPORT FORM</h1>  
  11.   
  12.         <img src="~/icon.png" width="15%" height="15%" />  
  13.         <div class="content__text">  
  14.   
  15.             <bp:Window IsDisplayed="{value: IsWindowDisplayed}"  
  16.                        HeaderText="Reporting form"  
  17.                        CloseOnEscape="false"  
  18.                        CloseOnOutsideClick="false">  
  19.   
  20.                 <p>  
  21.   
  22.                     <h1>Person report</h1>  
  23.                 </p>  
  24.                 <p>  
  25.                     <h4>Search by type:</h4>  
  26.                 <p />  
  27.                 <bp:CheckBox CheckedItems="{value: PersonTypes}"  
  28.                              Changed="{command: UpdatePersonList()}"  
  29.                              CheckedValue="{value: 1}" Text="Type A" />  
  30.                 <br />  
  31.                 <bp:CheckBox CheckedItems="{value: PersonTypes}"  
  32.                              Changed="{command: UpdatePersonList()}"  
  33.                              CheckedValue="{value: 2}" Text="Type B" />  
  34.                 </p>  
  35.   
  36.                 <p>  
  37.                     <h4>Search by text:</h4>  
  38.                 <p />  
  39.                 ID Number:  
  40.                 <bp:TextBox Text="{value: IdSearch}" Type="Number" class="page-input" Visible="{value: SearchByTextVisible}" />  
  41.                 <bp:Button Text="Search" Click="{command: SearchById()}" class="page-button" Visible="{value: SearchByTextVisible}" />  
  42.                 <p />  
  43.   
  44.                 <h4>Report:</h4>  
  45.   
  46.                 <bp:GridView DataSource="{value: Persons}" class="page-grid">  
  47.                     <Columns>  
  48.                         <bp:GridViewTextColumn Value="{value: Id}" HeaderText="Id" />  
  49.                         <bp:GridViewTextColumn Value="{value: FirstName}" HeaderText="Firstname" />  
  50.                         <bp:GridViewTextColumn Value="{value: LastName}" HeaderText="LastName" />  
  51.                         <bp:GridViewTextColumn Value="{value: NamePersonType}" HeaderText="Type" />  
  52.                     </Columns>  
  53.                     <EmptyDataTemplate>  
  54.                         There are no search results.  
  55.                     </EmptyDataTemplate>  
  56.                 </bp:GridView>  
  57.   
  58.             </bp:Window>  
  59.   
  60.             <bp:Button Text="LOAD REPORT"  
  61.                        Click="{command: IsWindowDisplayed = true}" />  
  62.   
  63.         </div>  
  64.   
  65.         <img src="/Resources/Images/tree.svg" class="content__background-image content__background-image content__background-image--right" />  
  66.     </div>  
  67. </dot:Content>  

Application Analysis

 
The first element we will analyze is the Window component, which represents a modal dialog box, as in HTML. This control allows us to customize directly from its attributes as the window will be displayed. If we were working with DotVVM's base controls, to achieve this functionality we would have to make use of some Javascript functionalities directly to set the window. In this example, the window title can be assigned. You can also customize certain properties, such as not allowing the window to close by pressing the Escape key or clicking outside the window box.
 
In this example, the Boolean attribute IsWindowDisplayed, according to its value true or false, will allow whether or not to display the settings window.
  1. <bp:Window IsDisplayed="{value: IsWindowDisplayed}"  
  2.             HeaderText="Reporting form"  
  3.             CloseOnEscape="false"  
  4.             CloseOnOutsideClick="false">  
Here is the definition of the IsWindowDisplayed attribute for the window display:
  1. public bool IsWindowDisplayed { get; set; }   
To display the window, a button is used. This button is also another of the Business Pack components. The premium version allows you to make certain customizations in terms of its styles, for example, enable/disable the button, assign an icon, among other functionalities: Button/2.0.
 
The result is as follows:
Website For Data Reporting With DotVVM Business Pack
 
Within this window, the most important item corresponds to the table that allows users registered in the database to be listed. For this purpose, we use the GridView element, another DotVVM control that allows us to create tables to represent specific data. As the main part, the control allows us to indicate the data source through the DataSource property, in this case, the data source is defined as follows in the ViewModel,
  1. public GridViewDataSet<PersonModel> Persons { get; set; } = new GridViewDataSet<PersonModel>();   
The GridViewTextColumn tag is used for a column definition. In this case, we can find the Id, FirstName, LastName, and Type columns. These names come from the data type of the data source, in this case, from the PersonModel model.
  1. <bp:GridView DataSource="{value: Persons}" class="page-grid">  
  2.     <Columns>  
  3.         <bp:GridViewTextColumn Value="{value: Id}" HeaderText="Id" />  
  4.         <bp:GridViewTextColumn Value="{value: FirstName}" HeaderText="Firstname" />  
  5.         <bp:GridViewTextColumn Value="{value: LastName}" HeaderText="LastName" />  
  6.         <bp:GridViewTextColumn Value="{value: NamePersonType}" HeaderText="Type" />  
  7.     </Columns>  
  8.     <EmptyDataTemplate>  
  9.         There are no search results.  
  10.     </EmptyDataTemplate>  
  11. </bp:GridView>  
One of the sub-tags in GridView is EmptyDataTemplate, which is also included in DotVVM base controls. This tag allows you to display some HTML content in case the list of items is empty. In the end, with GridView we will visualize something like this:
 
Website For Data Reporting With DotVVM Business Pack
 
All right, with this table, we have the most important part of the report. From this, we can add additional components to set search criteria for example, and update this table based on the search.
 
The first case is using the premium version of the DotVVM CheckBox control. As in HTML or any other design environment, the CheckBox has the role of a checkbox for selecting items in an option set. For this example, the goal is to have two checkboxes, which correspond to the types of people. Depending on the selection, either type A, type B, or both, the table of records will be updated in accordance with this decision.
 
In the view part, we find the CheckedItems property that stores the value of the items that are selected. We also find the Changed property, which allows you to specify the method that will perform the actions at the time this element is activated or disabled.
  1. <bp:CheckBox CheckedItems="{value: PersonTypes}"  
  2.                 Changed="{command: UpdatePersonList()}"  
  3.                 CheckedValue="{value: 1}" Text="Type A" />  
  4.   
  5. <bp:CheckBox CheckedItems="{value: PersonTypes}"  
  6.                 Changed="{command: UpdatePersonList()}"  
  7.                 CheckedValue="{value: 2}" Text="Type B" />  
In the method of updating the records in the table, if we select one of the two types, then we will query the database according to the defined service: PersonService, to get the list of people according to the selected id. With this list retrieved, we will update the database by re-setting the items in the GridView data source.
  1. Persons.Items.Clear();   
The result of using the CheckBox control is as follows:
 
Website For Data Reporting With DotVVM Business Pack
 
Another of the controls that allow us to continue adding functionality to the GridView to set the search criteria for this report is the TextBox and Button elements. In this case, these components can be used to search for something specific to the report through a text entry. To exemplify the use of these elements in your app, the controls are used to find a specific person based on their ID.
  1. ID Number:  
  2.   
  3. <bp:TextBox Text="{value: IdSearch}" Type="Number" class="page-input" Visible="{value: SearchByTextVisible}" />  
  4.   
  5. <bp:Button Text="Search" Click="{command: SearchById()}" class="page-button" Visible="{value: SearchByTextVisible}" /> 
Updating the elements of the GridView is similar to the CheckBox case. The result is as follows:
 
Website For Data Reporting With DotVVM Business Pack
 

What's Next?

 
In this article, we learned certain features of the Windows, GridView, CheckBox, TextBox, and Button components of DotVVM's Business Pack control set to display a list of data and set search criteria through the Model, View, ViewModel pattern in ASP.NET Core, and DotVVM.
 
The source code for this implementation is available in this repository: Reports in DotVVM with Business Pack controls.
 
Here are some additional resources that might be of interest to you to continue to gain new knowledge in this area,
If you have any concerns or need help in something particular, it would be a pleasure to be able to collaborate.
 
See you on Twitter!! :)


Similar Articles