Customizing the HtmlHelper method

Source: Internet
Author: User

Original: http://www.cnblogs.com/wenjiang/archive/2013/03/30/2990854.html

The HtmlHelper method is a very powerful feature in ASP. NET MVC, and with this feature, we can customize our own pages more freely.

There are usually three ways to customize your own HtmlHelper method, such as:

I. Razor syntax

The razor approach is very intuitive, like this:

@model ienumerable<musicshop.models.album>@{viewbag.title = "Index";} @helper Truncate (string input, int length) {if (input.    Length <= length) {@input; } else {@input.    Substring (0, length) <text>...</text>;        }}

@helper prompt the compiler, which is an auxiliary method, we can use the helper method using the @+methodname method.
Of course, in web programming, inline is a bad way, because they make the page load slower, especially if the page is loaded repeatedly. The more common way is to define a helper method in a namespace, and then introduce the helper method in the page so that the page does not have to bear the load of the method source code.

Two. Extension methods

In this way, we usually need to create a new folder, dedicated to our custom helper methods (many times, our custom helper methods may be many, but folders such as Controller,models are not suitable for storing these methods), In fact, it is specifically for the auxiliary method to establish a new namespace.

It's like this:

Using system;using system.collections.generic;using system.linq;using system.web.mvc;namespace MusicShop.Helpers{ Public    static Class Htmlhelpers    {public        static string Truncate (this htmlhelper helper, string input, int length)         {             if (input. Length <= length)             {                 return input;             }             else             {                 return input. Substring (0, length) + "...";}}             }     

Then add the following code to our page:

@model ienumerable<musicshop.models.album>@{viewbag.title = "Index";} @using musicshop.helpers

@using is necessary to introduce the namespace in which the helper method resides. For the processing of auxiliary methods, we can see that we use it as an extension method for HtmlHelper. The use of extension methods makes the originally very complex MVC design also have good readability, and flexibility and reusability is very high. When I first learned C #, I was not particularly cold about the extension method because it was easy to "contaminate" the namespace of the extended type, making it annoying to find the type method. However, when using MVC, especially the HtmlHelper method, the extension method gave me a great shock: if there is no extension method, the readability of the entire code is really scary! Then I think of C # 's design philosophy: Read Write code. Yes, using the extension method, our code can be read like a complete sentence by others and not be stopped in a difficult place.

Of course, the misuse of grammatical sugars is a dangerous behavior, after all, the extension method still exists I just said the problem, especially considering the future development of software, we will be surprised in a cold sweat: if a type in the future to add a method like my extension, then my code will be wrong.

So, we need a more secure approach.

Three. Razor View

We can create a new cshtml, like this:

@helper truncatestring (string input, int length) {    if (input. Length <= length) {        @input    } else {        @input. Substring (0, length) <text>...</text>    }}

Then there is our page:

@model ienumerable<musicshop.models.album>@{viewbag.title = "Index";}        

With this approach, there is no razor syntax inline, no extended method of worry, and we are free to add new custom methods, but note that helpers.cshtml must be placed in App_Code.

Customizing the HtmlHelper method

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.