CodeProject
We have seen several examples on MVC Razor in earlier posts. In every code module we demonstrated until now,
@Html.SomeFunction() made our lifes easy with MVC. These are nothing but Html Helper classes.
HTML Helper classes:
An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML
<input> and
<img> tags. You also can use HTML Helpers to render more complex content such as a tab strip or an HTML table of database data.
The ASP.NET MVC framework includes the following set of standard HTML Helpers (this is not a complete list):
- Html.ActionLink()
- Html.BeginForm()
- Html.CheckBox()
- Html.DropDownList()
- Html.EndForm()
- Html.Hidden()
- Html.ListBox()
- Html.Password()
- Html.RadioButton()
- Html.TextArea()
- Html.TextBox()
We can even write our own helper classes to perform common & complex tasks with ease.
Immediately a question pops up into your mind.
Why Helper classes, why not a DLL where i can use through out the project?
Yes, you can do that, but with helper class you can do better. Let me explain you. If you use the function as DLL, you might use it in model or Controller. Thus you are making that functionality specific to that controller or Model or may be view. But if you use it as a helper class, it can be used just like any html helper class through out the project. In future if you changed the helper method name, it requires no code change in Controller or Model. Search the old method name and replace it with new method name just like any html tag.
Enough with theory, lets jump in to practice.