.Net MVC @helper in Razor view using C# and VB

Since ASP.Net MVC3, the Razor View Engine has been introduced to generate the HTML views. When you have started making MVC applications, you’ve definitely used this already and are familiar with creating logic and accessing the @Model in .vbhtml or .cshtml files. But have you heard of @Helper to create reusable pieces of View?

Using helpers

Sometimes there will be bits of html or strings that would return in different pages which aren’t complex enough to dedicate a whole partial view for. For example, the translation of a word or a class that has to be filled in on a certain occasion. For these tiny bits of returning html, you can create helpers within your view.

A helper can easily be defined and used within your view like so:

Creating helpers to use on multiple views

This is nice and easy to create and use. But now you want to use the same little helper on all your views. However, it is bad practice to paste the same piece of code over and over again. So you want to define your helpers where you can call them up in every view you create.

To do this, you’ll have to create a .cshtml (or .vbhtml) file in the “App_Code” directory of your project. Here you will be able to create a library of helpers which you can reuse throughout your whole project.

You then just simple write the helper as you did in the regular view:

Or when you are using VB.Net:

As you can see, VB.Net requires the ‘@’ symbols much more than the C# version of Razor View. So C# improves the readability of your HTML code, although VB.Net reads more like a piece of program flow.

The helpers you have created will be available in the ‘Helpers’-class (depends on how you named the file) after compilation. Visual Studio will also provide you with intellisense when creating a call to the requested helper.

Calling a helper within logic in a view

You can also use helpers within your codeblocks in a Razor View .cshtml file. It’s as easy as using them within the html tags of the view. But be warned! The helpers return an object of the HelperResult class! So if you want to use these in code, don’t forget to call a .ToString() function on them to get the raw HTML out of it.

 

Leave a Reply

Your email address will not be published. Required fields are marked *