Web Forms With DotVVM Controls


Currently, web pages allow internet users to learn about a particular product or service or how to contact a company, but they can also help collect information about its users and thus establish a data source. This is an important tool for these purposes in the form.
For the design of a form, the most basic way is to use HTML tags to obtain certain required information. Then, it is necessary to use some programming language through some framework to process that information.
With these considerations in mind, in order to fulfill the purpose of collecting and performing certain procedures with a form, we will use DotVVM in this article through ASP.NET Core.

DotVVM Controls

Depending on the technology you are working with, extracting information entered into a form can be carried out through different mechanisms. In the case of DotVVM, the framework has a number of controls or components for various purposes that allow you to design elements for the implementation of forms through HTML and C#.
This communication between HTML (web pages) and C# (source code) is done through the MVVM design pattern (Model, View, Viewmodel). The purpose of these elements are as follows:
  • The model - is responsible for all application data and related business logic.
  • The view - gives 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.
Next, we'll look at an example to observe how DotVVM controls work.

DotVVM Form

Web Forms With DotVVM Controls
Considering that a web page in DotVVM consists of a view and also a view-model, let's analyze each of these elements separately.
  1. public class DefaultViewModel : MasterPageViewModel  
  2. {  
  3.     public string Title { getset;}  
  4.     public PersonModel Person { getset; } = new PersonModel { EnrollmentDate = DateTime.UtcNow.Date };  
  7.     public DefaultViewModel()  
  8.     {  
  9.         Title = "Person Form";  
  10.     }  
  12.     public void Process()  
  13.     {  
  14.         String script = "alert('" + "Welcome" + " " + Person.Username + " to Web App :) ')";  
  15.         Context.ResourceManager.AddStartupScript(script);  
  16.     }  
  18. }  
 For starters, in the model view of the web page, we find two attributes, Title for the page title, and 'Person' an object with the properties of the Person class. 
  1. public string Title { getset;}  
  2. public PersonModel Person { getset; } = new PersonModel { EnrollmentDate = DateTime.UtcNow.Date };  
In this class, PersonModel, there is an important aspect to mention, these are annotations, in this case, annotations of type [Required], which allow you to set as validation that the specified attributes cannot be null. We can also find other directives to validate the data type of a variable, handle regular expressions, among other cases. For more information on policy validation, we can consult the following source.
Returning to the analysis of the DefaultViewModel, we also find the constructor in which objects and attributes can be initialized:
  1. public DefaultViewModel()  
  2. {  
  3.     Title = "Person Form";  
  4. }  
Finally, in this view model, we have a Process method, which aims to process the input of information into a form:
  1. public void Process()  
  2. {  
  3.     String script = "alert('" + "Welcome" + " " + Person.Username + " to Web App :) ')";  
  4.     Context.ResourceManager.AddStartupScript(script);  
  5. }  
In this case, as an exemplification, the AddStartupScript method is used to use a JavaScript statement to launch a message in an alert.
  1. @viewModel DotVVMControls.ViewModels.DefaultViewModel, DotVVMControls  
  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.         <div Validator.Value="{value: Person.Username}"  
  14.              Validator.InvalidCssClass="has-error"  
  15.              Validator.SetToolTipText="true"  
  16.              class="page-input-box">  
  17.             <b>Username:</b>  
  18.             <br />  
  19.             <dot:TextBox Text="{value: Person.Username}" style="border: 1px solid #4a4d55; font-size: 1.1em;" />  
  20.         </div>  
  22.         <p />  
  24.         <div Validator.Value="{value: Person.EnrollmentDate}"  
  25.              Validator.InvalidCssClass="has-error"  
  26.              Validator.SetToolTipText="true"  
  27.              class="page-input-box">  
  28.             <b>EnrollmentDate:</b>  
  29.             <br />  
  30.             <dot:TextBox Text="{value: Person.EnrollmentDate}"  
  31.                          ValueType="DateTime"  
  32.                          FormatString="dd/MM/yyyy"  
  33.                          class="page-input"  
  34.                          style="border: 1px solid #4a4d55; font-size: 1.1em;" />  
  35.         </div>  
  37.         <p />  
  39.         <div Validator.Value="{value: Person.EnrollmentDate}"  
  40.              Validator.InvalidCssClass="has-error"  
  41.              Validator.SetToolTipText="true"  
  42.              class="page-input-box">  
  43.             <b>Gender:</b>  
  44.             <br />  
  45.             <dot:RadioButton id="Male" CheckedItem="{value: Person.Gender}" style="border: 1px solid #4a4d55; font-size: 1.1em;" style="border: 1px solid #4a4d55; font-size: 1.1em;" />  
  46.             <label for="Male">Male</label>  
  47.             <dot:RadioButton id="Female" CheckedItem="{value: Person.Gender}" style="border: 1px solid #4a4d55; font-size: 1.1em;" />  
  48.             <label for="Female">Female</label>  
  49.         </div>  
  51.         <p />  
  53.         <b>About:</b>  
  54.         <br />  
  55.         <dot:TextBox Text="{value: Person.About}" Type="MultiLine" class="page-input" style="border: 1px solid #4a4d55; font-size: 1.1em;" />  
  57.         <p />  
  59.         <dot:Button Text="Process" Click="{command: Process()}" class="page-button" style="background-color: #004C88; border: 2px solid ; color: #fff; font-weight: 600; padding-left: 2em; padding-right: 2em; font-size: 1rem;" />  
  61.         <p />  
  63.     </div>  
  65. </dot:Content>  
A first important aspect to mention is the display of the contents of a variable on the website.
  1. <b>{{value: Title}}</b>  
This is a DotVVM control called Literal, and it helps to render text on the page. More information here.
Another of the most useful controls in DotVVM is data binding. This allows us to manage values either to retrieve or assign information. One example is the following:
  1. <div Validator.Value="{value: Person.Username}"  
  2.              Validator.InvalidCssClass="has-error"  
  3.              Validator.SetToolTipText="true"  
  4.              class="page-input-box">   
Here, we see how the Username value of the Person object is being used. In this case, through another Validator control, which, as the name implies, allows validations to be performed on the form. For example, the validation is being given according to the annotations of the Username attribute, which for the time being has the annotation [Required] as we saw in the Person class.
Learn more about Validator here and more about Binding here.
Now, in relation to controls for form elements we can have the following example,
  1. <dot:TextBox Text="{value: Person.Username}" />  
In this case, the control is TextBox, which becomes the DotVVM version of the tag <input type'"text ... />, with the difference, that here we have the data-bind to load or get the information of an attribute, Pearson.Username in the case of the example.
Learn more about TextBox here.
If we continue along this same line, another control that we can find is the RadioButton, which follows the same principle as the HTML tag only here we can work with data-bind to communicate with the model of the view for the handling of the information.
  1. <dot:RadioButton id="Female" CheckedItem="{value: Person.Gender}" />  
  2. <label for="Female">Female</label>  
More information about RadioButton here.

What's Next? 

With this tutorial article, we learned how to create dynamic forms by implementing views and models of views with controls predefined by DotVVM to work with web pages.
The source code for this implementation can be found in this repository: DotVVM Form Controls.
Want to know the steps to create a DotVVM app? To do this, you can review this article: Steps to Create an MVVM (Model-View-ViewModel) application with DotVVM and ASP.NET Core.
Want to take your first steps towards developing web applications with ASP.NET Core and DotVVM? Learn more in this tutorial: DotVVM and ASP.NET Core: Implementing CRUD operations.
Thank you!

See you on Twitter!! :)