Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do custom HTML helper classes violate the ASP.NET MVC model?

On a related post I mentioned that I have found custom HTML helpers to be just that, helpful, when developing. For instance, when I need paging for a "grid" I have a custom helper that I can call Html.Pager().

Some have made a point that HTML helpers are a violation of the MVC model. Personally, I don't see it being any different than the existing helpers, such as Html.Textbox() or Html.ActionLink().

I'm still trying to learn more about MVC, so all perspectives are appreciated.

like image 812
Papa Burgundy Avatar asked Dec 29 '08 23:12

Papa Burgundy


People also ask

What is custom HTML helpers in MVC?

HTML Custom Helpers HTML helper is a method that returns a HTML string. Then this string is rendered in view. MVC provides many HTML helper methods. It also provides facility to create your own HTML helper methods. Once you create your helper method you can reuse it many times.

What is HTML helper class in 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.

What is the role of HTML tag helper in ASP NET MVC?

An HTML Helper is just a method that returns a HTML 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>, <button> and <img> tags etc.

Should I use tag helpers or HTML helpers?

Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.


1 Answers

Notice that the existing helpers are all written as extension methods of the HtmlHelper class. We explicitly took that approach so that others can write their own helper methods as extension methods of HtmlHelper.

So in general, this is not a violation of the MVC model. I guess it really depends on what you are doing in your helper. Helpers should simply render html based on arguments passed into them. They shouldn't do any data access, etc...

They merely encapsulate code for rendering common pieces of markup. If you're doing that, then you're not in violation of the ASP.NET MVC model.

like image 149
Haacked Avatar answered Sep 21 '22 02:09

Haacked