Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC @helper syntax vs Html Helper Extension methods

I need to create custom html helper method. As far as I know there are two ways:

  1. Use @helper razor syntax. http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx

  2. Create HtmlHelper extension method.

What solution is better and why? What are advantages and disadvantages?

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. Don't know maybe in MVC 4 it is possisble.

Please help.

like image 226
Piotr Czarnecki Avatar asked Jul 23 '13 09:07

Piotr Czarnecki


People also ask

What is the difference between tag helper vs HTML helper?

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.

What is MVC HTML helpers and its methods?

What is HTML Helper in ASP.NET MVC 5? HTML Helpers are methods that return a string. Helper class can create HTML controls programmatically. HTML Helpers are used in View to render HTML content. It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application.

Why we use HTML helpers in 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.


2 Answers

One benefit to @helper is that, since the code is within the view, you can make changes to it without having to recompile your code. This lets you tweak the helper quickly and easily during development. I personally prefer to keep markup within views if possible for this very reason.

A related benefit of @helper is that your HTML will benefit from intellisense, while writing HTML in C# code gets no such benefit.

Additionally, if you are writing a helper that is not meant to be re-usable outside of a single view, then @helper makes it easier to make that clear. An HtmlHelper extension method would be confusing if only one view is intended to use it (unless you manage to put it in a class and in a namespace where only that one view would ever see the extension method).

like image 89
Bochu Avatar answered Sep 18 '22 23:09

Bochu


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)
like image 25
Darin Dimitrov Avatar answered Sep 18 '22 23:09

Darin Dimitrov