HTML templates for repetitive elements in ASP.NET & DotVVM

Daniel Gomez Jaramillo
Apr 1 · 3 min read

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:

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.

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:

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.

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

DotVVM

DotVVM is an open source ASP.NET

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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