Web Forms With DotVVM Business Pack

Today, the innovation in .NET is to integrate all Microsoft development tools, libraries, languages, technologies, purposes, under the same framework.  In this sense, there are several tools and controls available for performing applications with .NET. These tools make it easy for us to build applications by using their already established controls and components. One of these tools is DotVVM.
In a previous article, we were able to learn how to design forms with the base controls that DotVVM offers: Web forms with DotVVM controls.
This time we will learn in the same way how to design web forms with DotVVM (with C# and HTML), but using the premium controls that DotVVM offers, in this case, called  Business Pack.

Design Pattern - Model, View, ViewModel - MVVM

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,
  • 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.

Acquire DotVVM Business Pack 

DotVVM Business Pack is nothing more than a private NuGet, in which, we can make use of the competent premiums already established for the construction of web applications in the business field.
To install the Business Pack version, in addition to obtaining the corresponding license (there is a trial version), it is necessary to perform a configuration of a few minutes to be able to use these functionalities. The steps to add the private NuGet can be consulted at the following link.

Create a DotVVM project with Business Pack in Visual Studio 2019 

To create a project with the Business Pack option in DotVVM from Visual Studio 2019, we'll start by creating the DotVVM project like any other,
Web Forms With DotVVM Business Pack
By specifying the name and continuing, the project initialization wizard will give us a number of options to add certain functionality to the project. In this case, the functionality we are interested in is the DotVVM Business Pack located in the DotVVM Commercial Extensions section,
Web Forms With DotVVM Business Pack

DotVVM Form with Business Pack

To exemplify the use of DotVVM Business Pack components, we have a small application like this,
Web Forms With DotVVM Business Pack
Web Forms With DotVVM Business Pack
Considering that this website in DotVVM consists of a view and its corresponding model view, let's look at the most important parts as a whole of these elements.
  1. @viewModel BPFormControls.ViewModels.DefaultViewModel, BPFormControls  
  2. @masterPage Views/MasterPage.dotmaster  
  3. <dot:Content ContentPlaceHolderID="MainContent">  
  5.     <h1 align="center">  
  6.         <img src="UserIcon.png" width="20%" height="20%" />  
  7.         <br />  
  8.         <b>{{value: Title}}</b>  
  9.     </h1>  
  11.     <div align="center">  
  13.         <bp:Window IsDisplayed="{value: IsWindowDisplayed}"  
  14.                    HeaderText="Form"  
  15.                    CloseOnEscape="false"  
  16.                    CloseOnOutsideClick="false">  
  17.             <h1 align="center"><p><b>Pearson Form</b>.</p></h1>  
  19.             <div Validator.Value="{value: Person.Username}"  
  20.                  Validator.InvalidCssClass="has-error"  
  21.                  Validator.SetToolTipText="true"  
  22.                  class="page-input-box">  
  23.                 <b>Username:</b>  
  24.                 <br />  
  25.                 <bp:TextBox Text="{value: Person.Username}"  
  26.                             UpdateTextAfterKeydown="true"  
  27.                             Type="Normal"  
  28.                             Placeholder="Insert your username..."  
  29.                             AutoFocus="true"  
  30.                             style="width: 80%;" />  
  31.             </div>  
  33.             <p />  
  35.             <div Validator.Value="{value: Person.EnrollmentDate}"  
  36.                  Validator.InvalidCssClass="has-error"  
  37.                  Validator.SetToolTipText="true"  
  38.                  class="page-input-box">  
  39.                 <b>EnrollmentDate:</b>  
  40.                 <br />  
  42.                 <bp:DateTimePicker SelectedDateTime="{value: Person.EnrollmentDate}"  
  43.                                    HourText="Hours"  
  44.                                    MinuteText="Minutes"  
  45.                                    SecondText="Seconds"  
  46.                                    AmPmDesignatorText="AM/PM"  
  47.                                    style="width: 80%;" />  
  49.             </div>  
  51.             <p />  
  53.             <div Validator.Value="{value: Person.EnrollmentDate}"  
  54.                  Validator.InvalidCssClass="has-error"  
  55.                  Validator.SetToolTipText="true"  
  56.                  class="page-input-box">  
  57.                 <b>Gender:</b>  
  58.                 <br />  
  60.                 <bp:RadioButton CheckedItem="{value: Person.Gender}"  
  61.                                 CheckedValue="Male"  
  62.                                 Text="Male" />  
  63.                 <bp:RadioButton CheckedItem="{value: Person.Gender}"  
  64.                                 CheckedValue="Female"  
  65.                                 Text="Female" />  
  66.             </div>  
  68.             <p />  
  70.             <b>About:</b>  
  71.             <br />  
  72.             <bp:TextBox Text="{value: Person.About}"  
  73.                         Type="MultiLine"  
  74.                         Placeholder="Enter information about you..."  
  75.                         class="page-input" style="width: 80%;" />  
  77.             <p />  
  79.             <b>Profile Picture:</b>  
  80.             <br />  
  81.             <bp:FileUpload Data="{value: ProfilePicture}"  
  82.                            AllowMultipleFiles="false"  
  83.                            UploadCompleted="{command: ProcessFile()}"  
  84.                            class="page-input" style="width: 80%;" />  
  86.             <p />  
  88.             <bp:Button Text="Process"  
  89.                        Click="{command: Process()}"  
  90.                        style="width: 80%;"/>  
  92.             <p />  
  94.         </bp:Window>  
  96.         <bp:Button Text="Start form"  
  97.                    Click="{command: IsWindowDisplayed = true}" />  
  99.     </div>  
  100. </dot:Content>  
  1. public class DefaultViewModel : MasterPageViewModel  
  2. {  
  3.     public string Title { getset; }  
  4.     public PersonModel Person { getset; } = new PersonModel { EnrollmentDate = DateTime.UtcNow.Date };  
  5.     public bool IsWindowDisplayed { getset; }  
  7.     public UploadData ProfilePicture { getset; } = new UploadData();  
  10.     public DefaultViewModel()  
  11.     {  
  12.         Title = "Person Form";  
  13.     }  
  15.     public void Process()  
  16.     {  
  17.         this.IsWindowDisplayed = false;  
  18.         String script = "alert('" + "Welcome" + " " + Person.Username + " to Web App :) ')";  
  19.         Context.ResourceManager.AddStartupScript(script);  
  20.     }  
  22.     public void ProcessFile()  
  23.     {  
  24.         // do what you have to do with the uploaded files  
  25.         String script = "alert('" + "ProcessFile() was called.')";  
  26.         Context.ResourceManager.AddStartupScript(script);  
  27.     }  
  28. }  
  1. public class PersonModel  
  2.     {  
  3.         [Required]  
  4.         public string Username { getset; }  
  5.         [Required]  
  6.         public DateTime EnrollmentDate { getset; }  
  7.         [Required]  
  8.         public string Gender { getset; }  
  9.         public string About { getset; }  
  10.     }  
The first element we will analyze is the Window component, which represents a modal dialog window, 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. We 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 of true or false, will allow whether or not to display the set window. 
  1. <bp:Window IsDisplayed="{value: IsWindowDisplayed}"  
  2.                    HeaderText="Form"  
  3.                    CloseOnEscape="false"  
  4.                    CloseOnOutsideClick="false">  
This is the definition of the IsWindowDisplayed attribute for the window display,
  1. public bool IsWindowDisplayed { getset; }   
To display the window, a button is used. This button is also another of the Business Pack components. The premium version allows us to make certain customizations in terms of its styles, for example, enable/disable the button, assign an icon, among other functionalities.
The result is as follows,
Web Forms With DotVVM Business Pack
When loading the window, we see how the cursor is positioned in the text box intended for the Person's Username attribute, this property is called AutoFocus, and is part of the premium component TextBox.
  1. <bp:TextBox Text="{value: Person.Username}"  
  2.             UpdateTextAfterKeydown="true"  
  3.             Type="Normal"  
  4.             Placeholder="Insert your username..."  
  5.             AutoFocus="true"  
  6.             style="width: 80%;" />  
Among the features of this component, one of the most relevant is the Type property. This allows us to specify whether the component is of type text, password, or textarea. For example, in the case of the application, the About section of the Person through the MultiLine type looks like this:
  1. <bp:TextBox Text="{value: Person.About}"  
  2.             Type="MultiLine"  
  3.             Placeholder="Enter information about you..."  
  4.             class="page-input" style="width: 80%;" />   
Continuing the analysis of this form, we will now review the DatePicker component, a premium control that allows us to work with dates and times through established designs. In this case, the component looks something like this,
Web Forms With DotVVM Business Pack
Business Pack components generally have a greater number of properties that allow us to customize controls. In this case, in the DateTimePicker, we can adjust for example the time format (AM/PM or 24-hour format), set icons, adjust the start day of the week, among others.
Finally, the last component we will analyze is FileUpload, another of the exclusive controls of the Business Pack tools that allows us to upload files from a form, to process them and fulfill some specific objective. In this case, the control can be useful for loading the profile picture of a particular user.
Web Forms With DotVVM Business Pack
To work with files, an image for example can be dragged to the upload section or through a search by means of the operating system directories. 
  1. <bp:FileUpload Data="{value: ProfilePicture}"  
  2.                 AllowMultipleFiles="false"  
  3.                 UploadCompleted="{command: ProcessFile()}"  
  4.                 class="page-input" style="width: 80%;" />  
There are certain functionalities that can be considered in this control, for example, specifying whether one or more files can be loaded through the AllowMultipleFiles property, performing a particular action when the file has been uploaded through the UploadCompleted property, among others.
What's next?
With this article, we learned certain features of DotVVM premium components through its Business Pack. We've also seen how to create dynamic forms by implementing views and models of views with DotVVM-defined controls for working with web pages.
The source code for this implementation is available in this repository: DotVVM Business Pack.
Additional resources
Want to know the steps to create a DotVVM app? To do this you can check this article: Steps to create an MVVM application (Model-View-Viewmodel) with DotVVM and ASP.NET Core.
Want to take your first steps in developing web applications with ASP.NET Core and DotVVM? Learn more in this tutorial: DotVVM and ASP.NET Core: Implementing CRUD operations.
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!! :)

Similar Articles