MVC 3 razor syntax-the @ helper syntax

Source: Internet
Author: User
Tags define null

Original article address:

Http://www.cnblogs.com/wbpmrck/archive/2011/10/13/2210338.html

 

 

1. Preface

I recently read an old Scott article and thought it was quite good. I tried to translate it,ArticleIt mainly describes how to use the @ helper syntax in razor to define reusable view template methods. If you have any omissions, please kindly advise them ~

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 brand new view engine called Razor (and continues to provide and enhance support for the existing. aspx view engine ). Razor minimizes the number of characters required to write a view template (front-end page) and the number of keyboard hits, providing a fast and smooth coding experience.

Unlike most template languages, Razor does not require starting or disabling tags to interrupt your encoding ), razor's syntax analyzer can intelligently identifyCode. The syntax is compact and concise, and the input is easy and pleasant.

You can read more about razor in my articles published over the past nine 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 mainly discusses a feature that is not well known: using the @ helper syntax to define reusable helper Methods

@ Helper syntax allows you to easily create reusable help methods in view templates to encapsulate some functions that are responsible for outputting to pages. With this technology, you can write code with better readability and abstract reusable help methods. Let's take a look at the simplest example:

First, let's look at a simple scenario that shows the product list: display the product name and price-if the product does not have a price, it will show "free"

We can see that the above code is very straightforward, and the razor syntax makes the combination of HTML and server C # code more natural and easy to understand.

There is only one point that looks awkward, that is, the "If else" logic for the price. If we need to display the product price on another page, we have to write the above judgment logic again. errors may occur in every place, making the code difficult to maintain and bloated.

In this case, the logic of Price determination needs to be extracted to form the Helper method.

Let's encapsulate the price judgment logic and use a "displayprice" helper method to represent it:

We have defined a reusable displayprice method using the @ helper syntax, just like the standard C #/VB code, it can contain any number of parameters (you can also define null parameters ). Unlike standard C #/VB code, you can write HTML and other syntaxes supported by razor in addition to server code.

You can call them like calling Standard C # Or VB code.

Vs will also generate smart notification information for it:

The following describes how to share a helper among multiple view pages:

In the preceding example, we define our own helper method, which is in the same view template as the calling code. In fact, we can define the method outside the view template and make it public by all view templates.

To implement this function, we need to put the file defining the Helper method under the app_code directory. This directory should usually be under the project root directory.

For example, I created a file called "scottgu. cshtml" and defined two methods in it (you can define any number of helper methods in a file ):

Once we define these methods at the app layer, we canProgramUse them in any view template.

The scottgu. cshtml template file is automatically compiled into a class called scottgu and contains two static methods: "displayprice" and "anotherhelper ".

We can call them as follows:

Visual Studio provides us with smart sensing tips:

The translation is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.