I have been talking about the ASP.NET MVC Futures, Microsoft.Web.Mvc Assembly, during the past couple of posts as I wonder what is in the package:
One of the interesting namespaces that caught my eye is Microsoft.Web.Mvc.Controls, which has a few ASP.NET MVC Controls to whet your appetite for the future. One of the cooler MVC controls is the Repeater Control, which I appreciate in ASP.NET Webforms due to its lightness. This ASP.NET MVC version looks, feels, and smells similar to the webform's version in that it uses the concept of templates.
Registering ASP.NET MVC Controls
Go ahead and reference the ASP.NET MVC Futures Assembly in your application and add an item in your web.config to register the controls:
<pages>
<controls>
// ...
<add tagPrefix="mvc" namespace="Microsoft.Web.Mvc.Controls" assembly="Microsoft.Web.Mvc"/>
</controls>
</pages>
ASP.NET MVC Repeater Control
Add the MVC Repeater Control in your application. In this case I am using it in the HomeController's Index View to keep things simple:
<mvc:Repeater Name="products" runat="server">
<EmptyDataTemplate>
<li>No products.</li>
</EmptyDataTemplate>
<ItemTemplate>
<li><%# Eval("Title") %></li>
</ItemTemplate>
</mvc:Repeater>
Right off the bat I am wondering why they didn't implement Header and Footer Templates, but I will have to dig into it at a later time.
Pay attention to the Name Attribute on the Repeater, Name=”products”, as this appears to be the key in ViewData for which the MVC Repeater Control gets its values. That is a little odd, but I can work with it for now.
ASP.NET MVC Controller Action to Populate Repeater
Modify the HomeController's Index Action to populate the ViewData with a list of products:
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
IList<Product> products = new List<Product>()
{
new Product {Title = "Gyokuro"},
new Product {Title = "Sencha"},
new Product {Title = "Matcha"}
};
ViewData["products"] = products;
return View();
}
Create a simple Product Class with a Title Property in the application. As you would expect, when you run the application you will get a list of products, like:
Conclusion
If you are interested in using the ASP.NET MVC Repeater Control and other controls in the ASP.NET MVC Futures Assembly, download it and play with it yourself.
We will talk about other ( and better ) ways to pull this off in the future as well :)
David Hayden
ASP.NET MVC Resources