Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC Html Helper Extension

What is the best way to extend Html Helper TextboxFor? Is there a way to reuse the defautl implementation?

like image 835
JAS Avatar asked Sep 29 '11 22:09

JAS


People also ask

How can create HTML helper in ASP.NET MVC?

Creating HTML Helpers with Extension Methods If you want to create HTML Helpers that work just like the standard HTML Helpers included in the ASP.NET MVC framework then you need to create extension methods. Extension methods enable you to add new methods to an existing class.

What is HTML helper in ASP.NET MVC?

HTML Helpers are managed within View to execute HTML content. We can use HTML Helpers to implement a method that returns a string. This tutorial discusses how you can define or create custom HTML Helpers that you can use within your MVC views.

How do you create an HTML helper?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

What is the main purpose of HTML helper of ActionLink in ASP.NET MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.


1 Answers

You can create your extension methods (in a static class), for example:

public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {
            // call original method
            MvcHtmlString result = InputExtensions.TextBoxFor(helper, expression);
            // do modification to result
            return result;
        }
like image 186
Massimo Zerbini Avatar answered Sep 30 '22 01:09

Massimo Zerbini