There are two ways of creating a custom HTML helper in ASP.NET MVC:
- You can create a class with a static method that outputs HTML, or
- You can add an extension method to the HtmlHelper class.
Option #1 seems simpler and easier.
- What’s the advantage to Option #2?
- When would I want to do that instead of Option #1?
- Does Option #2 give you any sort of benefit or added features?
Edited to add :
In this particular situation, I’m trying to output a string that’s formed through a bunch of conditional logic. It seems wrong to put this in my model, but doing it all in Razor seems tedious and unnecessary — or am I wrong about that?
On our current project we started by doing lots of HtmlHelper extensions but we realize that’s just the wrong thing to do, mostly because:
So we simply throw out almost all of those helpers and we’ve implemented ViewModels and Templates for all of the controllers. That’s the correct way of doing it.
This is the best place to start: ASP.NET MVC 2 Templates, Part 1: Introduction
Bottom line, if you’re building HTML through code, something is wrong with your project’s logic/pattern.