Reports In DotVVM With GridView, CheckBox, And TextBox Controls

A table is a set of organized cells within which we can host different contents. This type of element is quite useful when making a report to display some specific information.
In previous articles on ASP.NET Core and DotVVM, we could generally see how to use predefined controls of these tools for data visualization as a report. Here are some of those articles:
Also, when we manage forms to manage information, we need to create tables before we can view the processed data. Here are some previous articles in case you want to learn how to design HTML forms,
This time, we will learn the basics of visualizing certain data and establishing some search criteria with C# and HTML, through DotVVM's controls in ASP.NET Core.
 
Note
The project source code that we'll discuss in this article can be found in this GitHub repository: DotVVM Reports.
 

Design Pattern - Model, View, ViewModel - MVVM

 
An important aspect to mention is on the basis of DotVVM. This framework is based on the Model, View, View model over .NET design pattern for communication between HTML (web pages) and C- (source code). The purpose of these parts are as follows,
  • The 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 the 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.

Report with ASP.NET Core and DotVVM

 
To exemplify the use of some DotVVM controls for reporting, we have a small application like this,
 
Reports In DotVVM With GridView, CheckBox, And TextBox Controls
 
Considering the design pattern MVVM – Model, View, ViewModel, we will analyze in general each of these parts for this project.
 
Model
 
Considering that the application data and related business logic is handled in this section, then we'll see how the corresponding data and services are handled.
 
The database consists of two tables: Person and PersonType.
 
Reports In DotVVM With GridView, CheckBox, And TextBox Controls
 
The SQL statements for creating these tables, their attributes, and inserting some records are as follows,
  1. CREATE SCHEMA IF NOT EXISTS `db` DEFAULT CHARACTER SET utf8;  
  2. USE `db` ;  
  3.   
  4. CREATE TABLE IF NOT EXISTS `db`.`PersonType` (  
  5.   `Id` INT NOT NULL,  
  6.   `Name` VARCHAR(45) NOT NULL,  
  7.   `Description` VARCHAR(45) NOT NULL,  
  8.   PRIMARY KEY (`Id`))  
  9. ;  
  10.   
  11. CREATE TABLE IF NOT EXISTS `db`.`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 `db`.`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 data access layer part refers to the definition of 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, we need to install three NuGet packages,
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools
  • MySql.Data.EntityFrameworkCore
Now, we 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 ready. 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 { getset; }  
  4.     public string FirstName { getset; }  
  5.     public string LastName { getset; }  
  6.     public int IdPersonType { getset; }  
  7.     public string NamePersonType { getset; }  
  8. }  
  1. public class PersonTypeModel  
  2. {  
  3.     public int Id { getset; }  
  4.     public string Name { getset; }  
  5.     public string Description { getset; }  
  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,
 
Reports In DotVVM With GridView, CheckBox, And TextBox Controls\
 
ViewModel 
  1. public class DefaultViewModel : MasterPageViewModel  
  2. {  
  3.     private readonly PersonService personService;  
  4.     public GridViewDataSet<PersonModel> Persons { getset; } = new GridViewDataSet<PersonModel>();  
  5.     public List<int> PersonTypes { getset; } = new List<int>();  
  6.     public string IdSearch { getset; }  
  7.     public bool SearchByTextVisible { getset; } = false;  
  8.   
  9.     public DefaultViewModel(PersonService personService)  
  10.     {  
  11.         this.personService = personService;  
  12.     }  
  13.   
  14.     public override async Task PreRender() {}  
  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.                 Persons.Items = await personService.GetAllPersonsAsync();  
  50.             }  
  51.         }  
  52.         else if (PersonTypes.Count == 1)  
  53.         {  
  54.             if (!string.IsNullOrEmpty(IdSearch))  
  55.             {  
  56.                 int IdPersonType = PersonTypes.FirstOrDefault();  
  57.                 List<PersonModel> list = new List<PersonModel>(); ;  
  58.                 list.Add(await personService.GetPersonByIdAndTypeAsync(Int32.Parse(IdSearch), IdPersonType));  
  59.                 Persons.Items = list;  
  60.             }  
  61.             else  
  62.             {  
  63.                 int IdPersonType = PersonTypes.FirstOrDefault();  
  64.                 Persons.Items = await personService.GetAllPersonsByTypeAsync(IdPersonType);  
  65.             }  
  66.         }  
  67.     }  
  68. }  
View
  1. <dot:Content ContentPlaceHolderID="MainContent">  
  2.     <div class="page-center">  
  3.         <div class="page-grid-top">  
  4.             <div class="student-image"></div>  
  5.             <h1>Person Report</h1>  
  6.         </div>  
  7.   
  8.         <p>  
  9.             <h4>Search by type:</h4>  
  10.             <p />  
  11.             <dot:CheckBox CheckedItems="{value: PersonTypes}"  
  12.                       Changed="{command: UpdatePersonList()}"  
  13.                       CheckedValue="{value: 1}" Text="Type A" />  
  14.             <br />  
  15.             <dot:CheckBox CheckedItems="{value: PersonTypes}"  
  16.                       Changed="{command: UpdatePersonList()}"  
  17.                       CheckedValue="{value: 2}" Text="Type B" />  
  18.         </p>  
  19.   
  20.         <p >  
  21.             <h4>Search by text:</h4>  
  22.             <p />  
  23.             ID Number:  
  24.             <dot:TextBox Text="{value: IdSearch}" Type="Number" class="page-input" Visible="{value: SearchByTextVisible}" />  
  25.             <dot:Button Text="Search" Click="{command: SearchById()}" class="page-button" Visible="{value: SearchByTextVisible}" />  
  26.         <p />  
  27.   
  28.         <h4>Report:</h4>  
  29.   
  30.         <dot:GridView DataSource="{value: Persons}" class="page-grid">  
  31.             <Columns>  
  32.                 <dot:GridViewTextColumn ValueBinding="{value: Id}" HeaderText="Id" />  
  33.                 <dot:GridViewTextColumn ValueBinding="{value: FirstName}" HeaderText="Firstname" />  
  34.                 <dot:GridViewTextColumn ValueBinding="{value: LastName}" HeaderText="LastName" />  
  35.                 <dot:GridViewTextColumn ValueBinding="{value: NamePersonType}" HeaderText="Type" />  
  36.             </Columns>  
  37.             <EmptyDataTemplate>  
  38.                 There are no search results.  
  39.             </EmptyDataTemplate>  
  40.         </dot:GridView>  
  41.   
  42.     </div>  
  43. </dot:Content>  

Web application analysis

 
In the following GIF, we can see in a general way the interaction with this small web page.
 
The first element we will analyze is the GridView, a DotVVM control that allows us tables to represent specific data. This component allows us to specify the data source through the DataSource property, in this case, the data source is defined as follows:
  1. public GridViewDataSet<PersonModel> Persons { getset; } = new GridViewDataSet<PersonModel>();  
The data source, in addition to a list of type GridViewDataSet, can also be defined through another collection of type List. The GridViewTextColumn tag is used for a column definition. In this case, we can find the columns Id, FirstName, LastName and Type. These names come from the data type of the data source, in this case, from the PersonModel model.
  1. <dot:GridView DataSource="{value: Persons}" class="page-grid">  
  2.     <Columns>  
  3.         <dot:GridViewTextColumn ValueBinding="{value: Id}" HeaderText="Id" />  
  4.         <dot:GridViewTextColumn ValueBinding="{value: FirstName}" HeaderText="Firstname" />  
  5.         <dot:GridViewTextColumn ValueBinding="{value: LastName}" HeaderText="LastName" />  
  6.         <dot:GridViewTextColumn ValueBinding="{value: NamePersonType}" HeaderText="Type" />  
  7.     </Columns>  
  8.     <EmptyDataTemplate>  
  9.         There are no search results.  
  10.     </EmptyDataTemplate>  
  11. </dot:GridView>  
Another gridview sub-tag is EmptyDataTemplate. This tag allows us to display some HTML content in case the list of elements is empty. In the end, with the GridView we will visualize something like this,
 
Reports In DotVVM With GridView, CheckBox, And TextBox Controls
 
Learn more about the GridView control here.
 
However, from this table there are several operations that can be performed, for example, to set additional components to create search criteria and update this table based on the search.
 
The first case is using a DotVVM CheckBox. 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 selected items. We also find the Changed property, which allows us to specify the method that will perform the actions when this element is activated or disabled.
  1. <dot:CheckBox CheckedItems="{value: PersonTypes}"  
  2.             Changed="{command: UpdatePersonList()}"  
  3.             CheckedValue="{value: 1}" Text="Type A" />  
  4. <br />  
  5. <dot:CheckBox CheckedItems="{value: PersonTypes}"  
  6.             Changed="{command: UpdatePersonList()}"  
  7.             CheckedValue="{value: 2}" Text="Type B" />   
In the update method, for example, 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. int IdPersonType = PersonTypes.FirstOrDefault();  
  2. Persons.Items = await personService.GetAllPersonsByTypeAsync(IdPersonType);  
  3. SearchByTextVisible = true;  
Something similar will happen if we don't select any of the checkboxes. In this case, we will only clean the items of the GridView.
  1. Persons.Items.Clear();  
As we saw in the GIF, the result of using the CheckBox control is as follows: 
Reports In DotVVM With GridView, CheckBox, And TextBox Controls
Learn more about the CheckBox control here.
 
Another of the controls that allow us to continue adding functionality to the GridView to set search criteria to this report are 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, in this application, controls are used to find a specific person according to their Id.
  1. <dot:TextBox Text="{value: IdSearch}" Type="Number" class="page-input" Visible="{value: SearchByTextVisible}" />  
  2.   
  3. <dot: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,
 
Reports In DotVVM With GridView, CheckBox, And TextBox Controls
 
All the information about the TextBox control can be found in here. And the button control in here.
 
What's next?
 
With this article, we learned certain features of the GridView, CheckBox, TextBox, and Button components to display a list of data and set search criteria through the Model, View, ViewModel design pattern in ASP.NET Core, and DotVVM.
 
The source code for this implementation is available in this repository: DotVVM Reports.
 
Additional resources
 
Want to continue to acquire new knowledge about ASP.NET Core and DotVVM? these resources might be of interest to you,
Thank you!:
 
If you have any concerns or need help in something particular, it will be a pleasure to be able to help.
 
See you on Twitter!! :)