What solution is better and why?
It depends.
What are advantages and disadvantages?
- Pros of custom HtmlHelper extension:
- It will work no matter what view engine you are using
- It is unit testable
- It is portable between applications
- Cons of custom HtmlHelper extension:
- Could become cumbersome to write lots of HTML logic in C#
- Pros of
@helper
:- Haven't seen any, I never use it
- Cons of
@helper
:- Haven't seen any, I never use it
Actually the thing is that @helper
is IMHO completely useless. When you want the advantages I mentioned about a custom HtmlHelper extension, you, well, build a custom HtmlHelper extension.
And if you are confronted to some of the disadvantages I mentioned about the custom HtmlHelper extension, you, well, use a partial view.
I only read that in MVC 3 when @helper is created globally in seperate .cshtml file it's impossible to use other build-in html helpers.
That's wrong. You could perfectly fine use other Html helpers. You just have to pass them as parameters:
@helper FooBar(HtmlHelper html) {
feel free to use the html helper here
}
and when consuming from a view:
@HelperName.FooBar(Html)