MVC Razor Helpers using Delegates

Once in a while, I find myself implementing the same helper functions in different pages. In an effort to not repeat myself, I generally move helper functions into a common cshtml file in App_Code (as suggested here. While this works great for a single application, I’ve found myself copying helpers from project to project.

So in order to avoid the copy-paste nightmare, I decided to see if I could abstract the basic functionality into an extension method and then have application specific rendering. This concept is similar to writing templated server controls in a web forms environment.

The first step is to create a new class library and add a static class to it. I called my class HtmlHelperExtensions.cs

One particular helper I use a lot is rendering something based on a condition. For example, show a logout button if the user is logged in, or render items in there are any. The following methods can be used to accomplish this.

Both of these methods are extensions for the HtmlHelper class (the one you’re using when you write Html.SomeMethod() in your views). The first one takes a template, an IEnumerable of the type and a condition. The second is simply a little syntactic sugar to allow passing a single item instead of an IEnumerable.

Here are some example calls for these functions.

Let’s assume you want to render something if the user is in a specific role. You could accomplish this by adding some more sugar.

Hopefully someone will find this of some use. Let me know what you think!