0

Simple question;

When are Custom HTML Helpers supposed to be used. Are we supposed (as intended by the developers of MVC) to implement an Extention method for all tags/logic that are reused across the views?

I'm curious about this, as we currently have a medium sized project that needs to be refactored. Alot of different interns have been working on this, and its time to do a cleanup.

Kristian Nissen
  • 1,153
  • 2
  • 11
  • 29
  • 2
    See [When should we use Html Helpers, Razor Helpers or Partial Views?](http://stackoverflow.com/questions/18046236) – Max Toro Oct 09 '14 at 13:52
  • possible duplicate of [When should we use Html Helpers, Razor Helpers or Partial Views?](http://stackoverflow.com/questions/18046236/when-should-we-use-html-helpers-razor-helpers-or-partial-views) – Luizgrs Oct 09 '14 at 13:56

2 Answers2

2

Use Html Helper for rendering an Html Element. That element may contain inner elements, but your helper should mean the outermost element. Let me give you an example. Say, you want to render an Image button with some specific attributes so that your image button will look like this:

<button title="my button" onclick="onClickFunctionName()">
  <img src="...." />
</button>

So, an Html Helper with following signature can be very useful:

@Html.ImageButton(title:"my button", onclickFunctionName:"onClickFunctionName", imageSource:"....")

Same like an image button, you can encapsulate creating a Table using Html Helper.

If you want to encapsulate a piece of html markup for some partial layout, then you should use Partial Views. For example, a Partial View can be used for rendering Login box with User name and Password and a Remember Me Checkbox. In this case, an HTML Helper wont be a good idea to use.

1

In situations where you want your links to work with your routing configurations. You use HTML property. It's instance of IHtmlHelper which contains methods for generating snippets of html. You could type them by hand, but this html property has a logic behind the scenes to use some metadata or configurations in your application. Also, HTML Helpers know built-in conventions in the MVC framework.