HTML Templates For Repetitive Elements In ASP.NET And DotVVM

Hello! Nice to say hello. This time we will learn about one of the DotVVM controls to render HTML templates for elements in a given collection, this control is called Repeater.
 
Repeater is a tag that allows us to set a preset HTML template to render items from a given listing. In other words, this control repeats a template for each item in a referenced collection in a DataSource.
 

Example of a repeater

 
To see this DotVVM control in action, let's look at an example. Suppose we want a web page that displays a list of cities, and that each of the cities displayed contains an image, the name of the city, and the country to which it belongs.
 
As it is well known, DotVVM is based on the MVVM pattern (Model, View, ViewModel), when using only C# and HTML. In this sense, let's look at each of these parts.
 

Model

 
The model is represented in this case by a CityModel class as follows,
  1. public class CityModel {  
  2.     public int Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string PhotoLink {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Country {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  

ViewModel

 
To assemble our list of cities, in this case, we will define in the ViewModel a collection with examples of cities, which we want to see later on our website.
  1. public List < CityModel > Cities {  
  2.     get;  
  3.     set;  
  4. } = new List < CityModel > ();  
  5. public DefaultViewModel() {  
  6.     Cities.Add(new CityModel {  
  7.         Id = 1, Name = "Salzburg", Country = "Austria", PhotoLink = "URL"  
  8.     });  
  9.     Cities.Add(new CityModel {  
  10.         Id = 9, Name = "Dubai", Country = "United Arab Emirates", PhotoLink = "URL"  
  11.     });  
  12.     Cities.Add(new CityModel {  
  13.         Id = 4, Name = "Vancouver", Country = "Canada", PhotoLink = "URL"  
  14.     });  
  15.     Cities.Add(new CityModel {  
  16.         Id = 5, Name = "La Paz", Country = "Bolivia", PhotoLink = "URL"  
  17.     });  
  18.     Cities.Add(new CityModel {  
  19.         Id = 7, Name = "Bonn", Country = "Germany", PhotoLink = "URL"  
  20.     });  
  21.     Cities.Add(new CityModel {  
  22.         Id = 10, Name = "Denver", Country = "United States", PhotoLink = "URL"  
  23.     });  
  24.     Cities.Add(new CityModel {  
  25.         Id = 8, Name = "Kochi ", Country = "India", PhotoLink = "URL"  
  26.     });  
  27.     Cities.Add(new CityModel {  
  28.         Id = 3, Name = "El Cairo", Country = "Egypt", PhotoLink = "URL"  
  29.     });  
  30.     Cities.Add(new CityModel {  
  31.         Id = 2, Name = "Washington", Country = "United States", PhotoLink = "URL"  
  32.     });  
  33. }  

View

 
With our Cities listing set, and with values included, we can now define our HTML template in the view. This template is as you can see below,
  1. <dot:Repeater DataSource="{value: Cities}" class="row">  
  2.     <ItemTemplate>  
  3.         <div class="col-lg-4 col-md-6">  
  4.             <div class="" data-aos="fade-up" data-aos-delay="100">  
  5.                 <img src="{{value: PhotoLink}}" alt="{{value: Name}}" class="img-fluid">  
  6.                 <div class="details">  
  7.                     <h3><a href="#" target="_blank">{{value: Name}}</a></h3>  
  8.                     <p>{{value: Country}}</p>  
  9.                 </div>  
  10.             </div>  
  11.         </div>  
  12.     </ItemTemplate>  
  13. </dot:Repeater>  
Where,
  • DataSource is to specify the list of items that we want to represent.
  • ItemTemplate, will be the HTML that is repeated for each of the items in the list.
  • Attributes, these can be located in the template according to the desired objective.
With these considerations, in the final we will be able to visualize something like this,
 
HTML Templates For Repetitive Elements In ASP.NET And DotVVM
 
Well, in this quick and easy way we can visually represent our elements of a list on a website.
 
The source code for this example is available in this GitHub repository: DotVVMRepeater.
 
Additional Resources
 
If you like to continue acquiring or reinforcing knowledge in this area, here are some additional resources that can be very useful,
Thank you very much for reading this article, I hope that this demo can be of help for the organization and dissemination of events. If you have any questions or ideas that you need to discuss, it will be nice to be able to collaborate and together exchange knowledge with each other.
 
If you like, we can stay in touch on Twitter, or in Telegram too. :)