Web Forms With Bootstrap For DotVVM From ASP.NET Core

Bootstrap is a framework originally created by Twitter, that allows you to create web interfaces with CSS and JavaScript, whose particularity is to adapt the website interface to the size of the device on which it is displayed. That is, the website automatically adapts to the size of a PC, Tablet or other device. This design and development technique is known as "responsive design" or adaptive design.
 
Bootstrap has several resources to configure the styles of page elements in a simple and efficient way, as well as facilitating the construction of pages that, at the same time, are adapted for the web and for mobile devices. Within the framework of ASP.NET Core, specifically DotVVM for web page design, there is a set of tools called Bootstrap for DotVVM, which allows us to work with ASP.NET Core and Bootstrap at the same time.
 

Model, View, ViewModelPattern

 
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 Bootstrap for DotVVM

 
Bootstrap for DotVVM is nothing more than a private NuGet for Visual Studio, in which, we can make use of the competent ones already established for the construction of web applications in the business field.
 
To install the Bootstrap for DotVVM version, in addition to obtaining the corresponding license (a trial version is available), it is necessary to configure a few minutes to be able to use these functionalities. The steps to add the private NuGet can be found on the following page: Installing Bootstrap for DotVVM.
 

Create a Bootstrap forDotVVM project in Visual Studio 2019

 
To create a project with the Bootstrap option in DotVVM from Visual Studio 2019, we will start by creating the project for this DotVVM Web Application (.NET Core),
 
Bootstrap for DotVVM 
 
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 Bootstrap for DotVVM located in the DotVVM Commercial Extensions section,
 
Bootstrap for DotVVM 
 

Web form with Bootstrap forDotVVM

 
To exemplify the use of certain Bootstrap components with DotVVVM we have a small web application like this,
 
Bootstrap for DotVVM 
 
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.
 
View
  1. <dot:ContentContentPlaceHolderID="MainContent">  
  2.   
  3.     <bs:PageHeader>  
  4.         <divalign="center"><h3align="center"><b>{{value: Title}}</b></h3></div>  
  5.     </bs:PageHeader>  
  6.   
  7.     <divalign="center">  
  8.         <bs:Form>  
  9.             <br/>  
  10.             <bs:PanelType="Primary"style="width:70%;">  
  11.                 <HeaderTemplate>  
  12.                     <div>  
  13.                         <br/>  
  14.                         <bs:ImageImageUrl="UserIcon.png"AlternateText="Persons"width="35%"height="35%"/>  
  15.                     </div>  
  16.                 </HeaderTemplate>  
  17.                 <ContentTemplate>  
  18.   
  19.                     <bs:FormGroup>  
  20.                         <divValidator.Value="{value:Person.Username}"  
  21.                             Validator.InvalidCssClass="has-error"  
  22.                             Validator.SetToolTipText="true"  
  23.                             class="page-input-box">  
  24.   
  25.                             <bs:TextBoxGroupLabelText="Username"  
  26.                                             Text="{value:Person.Username}"  
  27.                                             Type="Normal"/>  
  28.                         </div>  
  29.                     </bs:FormGroup>  
  30.   
  31.                     <bs:FormGroup>  
  32.                         <divValidator.Value="{value:Person.EnrollmentDate}"  
  33.                             Validator.InvalidCssClass="has-error"  
  34.                             Validator.SetToolTipText="true"  
  35.                             class="page-input-box">  
  36.                             <bs:DateTimePickerGroupSelectedDate="{value:Person.EnrollmentDate}"LabelText="EnrollmentDate"Mode="Date"/>  
  37.                         </div>  
  38.                     </bs:FormGroup>  
  39.   
  40.                     <bs:FormGroupLabelText="Gender">  
  41.                         <divValidator.Value="{value:Person.Gender}"  
  42.                             Validator.InvalidCssClass="has-error"  
  43.                             Validator.SetToolTipText="true"  
  44.                             class="page-input-box">  
  45.   
  46.                             <bs:RadioButtonText="Male"  
  47.                                             GroupName="RadioButtonsGenders"  
  48.                                             CheckedValue="Male"  
  49.                                             CheckedItem="{value:Person.Gender}"/>  
  50.                             <bs:RadioButtonText="Female"  
  51.                                             GroupName="RadioButtonsGenders"  
  52.                                             CheckedValue="Female"  
  53.                                             CheckedItem="{value:Person.Gender}"/>  
  54.                             <bs:RadioButtonText="Other"  
  55.                                             GroupName="RadioButtonsGenders"  
  56.                                             CheckedValue="Other"  
  57.                                             CheckedItem="{value:Person.Gender}"/>  
  58.   
  59.                         </div>  
  60.                     </bs:FormGroup>  
  61.   
  62.                     <bs:FormGroup>  
  63.                         <divValidator.Value="{value:Person.About}"  
  64.                             Validator.InvalidCssClass="has-error"  
  65.                             Validator.SetToolTipText="true"  
  66.                             class="page-input-box">  
  67.                             <bs:TextBoxGroupLabelText="About"  
  68.                                             Text="{value:Person.About}"  
  69.                                             Type="MultiLine"/>  
  70.                         </div>  
  71.                     </bs:FormGroup>  
  72.   
  73.                 </ContentTemplate>  
  74.                 <FooterTemplate>  
  75.                     <b>Bootstrap for DotVVM with ASP.NET Core demo</b>  
  76.                 </FooterTemplate>  
  77.             </bs:Panel>  
  78.         </bs:Form>  
  79.     </div>  
  80.   
  81. </dot:Content>  
 
Viewmodel
  1. public class DefaultViewModel : MasterPageViewModel  
  2. {  
  3.     public string Title { getset; }  
  4.     public Person Model Person { getset; } = new Person Model { EnrollmentDate = DateTime.UtcNow.Date };  
  5.   
  6.     public DefaultViewModel()  
  7.     {  
  8.         Title = "Person Form";  
  9.     }  
  10. }  
 
Model
 
The model for a person's data is defined as follows,
  1. public class PersonModel  
  2. {  
  3.     [Required]  
  4. public string Username { getset; }  
  5. public DateTime EnrollmentDate { getset; }  
  6. public string Gender { getset; }  
  7. public string About { getset; }  
  8. }   
The first Bootstrap element within the application is the PageHeader component, which represents the header of this web page.
  1. <bs:PageHeader>  
  2.     <divalign="center"><h3align="center"><b>{{value: Title}}</b></h3></div>  
  3. </bs:PageHeader>   
For the base format of the web form, the Form component is used. As the name implies, this element represents a Bootstrap form. For corresponding elements such as text boxes, RadioButton, LabelText, and so on the FormGroup control is used to indicate that these elements are part of this form. For example, for a form with a TextBox, the source code would look something like this:
  1. <bs:Form>  
  2.     <bs:FormGroupLabelText="TextBox">  
  3.         <dot:TextBoxText="{value: Text}"/>  
  4.     </bs:FormGroup>  
  5. </bs:Form>   
To display a visually friendly interface and to exemplify Bootstrap controls, the form is organized through the Panel component, which allows you to set a header, body, and footer.
 
In the header for example we can find in this case an image that has been created through the component Image,
 
Bootstrap for DotVVM 
  1. <HeaderTemplate>  
  2.     <div>  
  3.         <br/>  
  4.         <bs:ImageImageUrl="UserIcon.png"AlternateText="Persons"width="35%"height="35%"/>  
  5.     </div>  
  6. </HeaderTemplate>   
Within the contents of the panel are the form's own elements, each with a component of the base controls called Validator, which, as the name implies, allows validations on a form. For example, for a Person's Username attribute, validation is performed according to the annotations set in the Model of the Person class, [Required] for this attribute.
  1. [Required]  
  2. publicstring Username { get; set; }   
In the case of the Username attribute for a person, the TextBoxGroup component is used to visually represent it on the form. One of the attributes of this control is the LabelText, which allows you to set a label for this text box.
 
Bootstrap for DotVVM 
  1. <bs:FormGroup>  
  2.     <divValidator.Value="{value:Person.Username}"  
  3.             Validator.InvalidCssClass="has-error"  
  4.             Validator.SetToolTipText="true"  
  5.             class="page-input-box">  
  6.   
  7.         <bs:TextBoxGroupLabelText="Username"  
  8.                             Text="{value:Person.Username}"  
  9.                             Type="Normal"/>  
  10.     </div>  
  11. </bs:FormGroup>   
Following this sequence, the DateTimePickerGroup control is used for the EnrollmentDate field. Something interesting about this component is that it allows us to work with a different date, time, or combination formats. In the following image we can see an example of this,
 
Bootstrap for DotVVM 
 
In the case of the person's gender, the RadioButton component is used, which extends the default RadioButton control with additional Bootstrap features. In this case, the IsInline attribute is used, as it allows you to set whether there will be more radio buttons on the same line.
 
Bootstrap for DotVVM 
  1. <bs:RadioButtonText="Male"  
  2.                 GroupName="RadioButtonsGenders"  
  3.                 CheckedValue="Male"  
  4.                 CheckedItem="{value:Person.Gender}"  
  5.                 Inline="true"/>  
  6. <bs:RadioButtonText="Female"  
  7.                 GroupName="RadioButtonsGenders"  
  8.                 CheckedValue="Female"  
  9.                 CheckedItem="{value:Person.Gender}"  
  10.                 Inline="true"/>  
  11. <bs:RadioButtonText="Other"  
  12.                 GroupName="RadioButtonsGenders"  
  13.                 CheckedValue="Other"  
  14.                 CheckedItem="{value:Person.Gender}"  
  15.                 Inline="true"/>   
Finally, the person's About field is also used with the component of type TextBoxGroup as in the field of Username, the difference, in this case, is in the attribute Type, this time being of type MultiLine.
  1. <bs:TextBoxGroupLabelText="About"  
  2.                     Text="{value:Person.About}"  
  3.                     Type="MultiLine"/>  

What next?

 
With this article, we learned certain features of Bootstrap custom components for DotVVM. We have also seen how to create dynamic forms through the implementation of views and models with DotVVM-defined controls over ASP.NET Core from Visual Studio 2019 to work with web pages.
 
The source code for this implementation is available in this repository: Bootstrap for DotVVM.
 
Additional Resources
 
Want to continue to acquire new knowledge about ASP.NET Core and DotVVM? These resources may be of interest to you:
Thank you:
 
If you have any concerns or need help in something, it will be a pleasure to be able to help.
 
See you on Twitter!! :)