Posts

Showing posts from July, 2013

ASP.NET MVC Custom HTML Helpers (C#)

In this article we will understand the different ways of creating custom helper method in ASP.NET MVC4 application. Though we have built-in html helpers like Html.ActionLink(), Html.TextBox() and many more, some times we may need to have custom helper methods for example Image or a block of html controls which is commonly used across the application like Address fields. Custom HTML Helpers make it easier to generate view content. We can create custom helpers in three ways: 1. with   Static Methods 2. with   Extension Methods 3. with   @helper   in the razor view. Let's understand how to implement the same. 1. Static Methods: In this approach we will create a static method and use that method in the view, here its is ImageStatic method which will return HtmlString. CustomHelper.cs: namespace HelperApplication.Helpers { public static class CustomHelpers { //Static method public static IHtmlString ImageStatic( string src, string alt = "Ima