HTML templates for repetitive elements in ASP.NET & 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.

The model is represented in this case by a CityModel class as follows:

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.

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:

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:

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.

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 Facebook, on Twitter, or in Telegram too. :)

Microsoft Learn Student Ambassador

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store