MVC Custom HTML Helper Method

Kailash Chandra Behera | Sunday, September 18, 2016

Introduction

This article describes the difference ways of creating Custom HTML Helper Method and the type of HTML Helper Method in MVC.

Getting Started

AS per my knowledge everyone has idea of MVC HTML Helper Methods, hence I am not going to explain ‘What is Helper Methods’. If you don’t have idea about MVC Helper Methods, lot of sites are there please refer first that.

The standard HTML Helpers are very helpful but limited to some common scenario, it sometimes does not fulfills our requirements. Hence there is requirements of customizations of existing helper or to create custom helper. There are two very simple ways available to create Custom HTML Helper Methods.

  1. Extension Method on Html Helper Class Approach
  2. Static Methods Approach

Extension Method on Html Helper Class Approach


This way helps to use Custom HTML Helper Method like standard Helper method. It creates extension methods of HTML Helper class , hence user can use custom helper method with standard helper class like below code.
 @Html.CustomeTextBox(model=>model.Age)  

Static Methods Approach


This approach helps to create Custom HTML Helper by using Static Method, but as like above extension method you will not directly get this method with HTML Helper class in view page, you can get only with your static methods class like normally we use static method and you have to specify the namespace of the class in which your static method exists in web.config file as well. For example.
For adding namespace in config file.
 <add namespace=”MyCustomHelpers” />  
In View
 @CustomHelper.TextBox(“nameofinput”, “value of input”)  

Normaly there are two types of helper method as given below and Both the types of custom methods can be created with above mentioned approches, but difference is that the first type custom method supports model binding and the second type does not supports model binding.
  1. Strongly Type or Model Bound Helper Method
  2. Unbound Helper Mothed

Summary

In this article we have discussed the difference way are available to create Custome HTML Helper Method in MVC, hope this article may helpful to you.

Thanks