HTML templates for repetitive elements in ASP.NET & DotVVM
Tags: webdev, html, dotnet, csharp
Published: 2021-04-01 13:20:08

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:

```csharp
public class CityModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string PhotoLink { get; set; }
    public string Country { get; set; }
}
```

####**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.

```csharp
public List<CityModel> Cities { get; set; } = new List<CityModel>();

public DefaultViewModel()
{
	Cities.Add(new CityModel { Id = 1, Name = "Salzburg", Country = "Austria", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 9, Name = "Dubai", Country = "United Arab Emirates", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 4, Name = "Vancouver", Country = "Canada", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 5, Name = "La Paz", Country = "Bolivia", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 7, Name = "Bonn", Country = "Germany", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 10, Name = "Denver", Country = "United States", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 8, Name = "Kochi ", Country = "India", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 3, Name = "El Cairo", Country = "Egypt", PhotoLink = "URL" });
	Cities.Add(new CityModel { Id = 2, Name = "Washington", Country = "United States", PhotoLink = "URL" });
}
```

####**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:

```html
<dot:Repeater DataSource="{value: Cities}" class="row">
	<ItemTemplate>
		<div class="col-lg-4 col-md-6">
			<div class="" data-aos="fade-up" data-aos-delay="100">
				<img src="{{value: PhotoLink}}" alt="{{value: Name}}" class="img-fluid">
				<div class="details">
					<h3><a href="#" target="_blank">{{value: Name}}</a></h3>
					<p>{{value: Country}}</p>
				</div>
			</div>
		</div>
	</ItemTemplate>
</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:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuhgvjmwh08lr883r5x6.png)

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](https://github.com/esdanielgomez/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:

+ [Building a Dashboard with ASP.NET Core and DotVVM](https://dev.to/esdanielgomez/building-a-dashboard-with-asp-net-core-and-dotvvm-349).
+ [.NET 5: Deploying web apps with Azure App Service](https://dev.to/esdanielgomez/net-5-deploying-web-apps-with-azure-app-service-5349)
+ [Working with web services through ASP.NET Core and DotVVM (A step-by-step guide)](https://dev.to/dotvvm/working-with-web-services-through-asp-net-core-and-dotvvm-a-step-by-step-guide-2le).

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 [Facebook](https://www.facebook.com/esDanielGomez), on [Twitter](https://twitter.com/esDanielGomez), or in [Telegram](https://t.me/esdanielgomez) too. :)