Objective
Recently turned to a Scott's old article, feel very good, try to translate, the article is mainly about how to use the @helper syntax in razor to define a reusable view template method. If there are omissions, please also please crossing Guidance twos ~
Original address: Http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx
2. Text
ASP. NET MVC 3 provides a new view engine (View-engine) called Razor (which also continues to provide and enhances support for the existing. aspx view engine). Razor minimizes the number of characters and keystrokes required to write the view template (Translator note: Front page), providing a fast and smooth coding experience.
Unlike most template languages, using razor you don't need to use some start and close tags to interrupt your coding (translator Note: <%%> like aspx), Razor's parser can intelligently identify the code. The syntax is compact and concise, and the input is easy and enjoyable.
You can learn more about razor from some of the articles I have published in the past 9 months:
- Introducing Razor
- New @model keyword in Razor
- Layouts with Razor
- Server-side Comments with Razor
- Razor ' s @: and <text> syntax
- Implicit and Explicit code nuggets with Razor
- Layouts and Sections with Razor
Today's article focuses on a less well-known feature: Using the @helper syntax to define reusable helper methods
@helper syntax allows you to easily create reusable helper methods in a view template to encapsulate some of the functions that are responsible for outputting to the page. Using this technique, you will be able to write more readable code and abstract out reusable helper methods. Let's look at one of the simplest examples:
First look at a simple scenario that shows the product list: Display the product's name and price--"free" if the product does not have a price
You can see that the code above is straightforward, and the razor syntax makes it more natural and understandable to combine HTML with the server C # code.
There is only one place that looks awkward, which is the "If else" logic for the price. If we need to display the price of a product on a different page, we have to write the judgment logic again, and every place can be wrong, causing the code to be difficult to maintain and bloated.
In this case, the logic of judging prices needs to be drawn out to form helper methods.
Let's wrap up the price judgment logic and use a "displayprice" helper method to represent it:
We have used the @helper syntax to define a reusable Displayprice method that, like the standard C#/VB code, can contain any number of parameters (you can also define nullable type parameters). Unlike the standard C#/VB code, you can write HTML, and other razor-supported syntax, in addition to the server-side code.
You can call them just like standard C # or VB code
VS also generates smart hints for it:
Here's a discussion of how to share a helper method on multiple view pages:
In the example above, we define our own helper method, which is in the same view template as the calling code. We can actually define the method outside of the view template and let it be common to all view templates.
To achieve this, we need to place the file that defines the helper method in the App_Code directory. This directory should usually be in the project root directory.
For example, I created a file called "scottgu.cshtml" and defined 2 methods in it (you can define any number of helper methods in one file):
Once we have defined these methods at the app level, we can use them in any of the application's view templates.
The scottgu.cshtml template file is automatically compiled into a class called ScottGu, and contains 2 static methods: "Displayprice" and "Anotherhelper".
We can call them as follows:
Visual Studio provides IntelliSense hints for us:
Use of @helper