How to customize htmlhelper in MVC

Source: Internet
Author: User

Microsoft is very considerate, just like a small cotton jacket. Htmlhelper provided by Microsoft is enough for most developers, but some features may not meet the requirements of htmlhelper provided by Microsoft. Next we will talk about how to customize htmlhelper.

I don't know if other developers have noticed metadata when using htmlhelper objects. Internal metadata such:

As we can see, the next custom method is to extend the htmlhelper object.

 

Three elements of extension method implementation:

1. Static Class 2, static method 3, this keyword

1. Define a class first, for example, myhtmlhelper:

Using system; using system. Collections. Generic; using system. LINQ; using system. Web; namespace mvcapplicationfirstdome. Models {// static class public static class myhtmlhelper {// static method }}

2. Assume that the extension method is getspan. The function is to return the result after the parameter is encapsulated. The Code is as follows: note that using system should be referenced in the myhtmlhelper class. web. MVC; namespace

// Static method public static string getspan (this htmlhelper, string text) {return "<span style = 'color: red'> "+ TEXT +" </span> ";}

After the above two steps, the htmlhelper Extension Method getspan can be basically used. Next we will explain how to call it on the page.

Pay attention to the next namespace when calling: If the namespace of the extension method is different from the namespace of the page, you must reference the namespace of the extension method, otherwise, there is no way to call custom methods on the page.

The reference namespace is written as follows:

 

After referencing the namespace, you can call the custom Extension Method on the corresponding page. The Code is as follows:

The browsing effect is as follows:

Here we should first know the difference between <%: %> and <% = %>. The colon is after encode, and the equal sign is directly output.

Here, we have initially completed the htmlhelper extension method.

Have you found out what is the difference between our own extension method and what Microsoft originally provided when calling it?

1. namespace to be referenced

2. Only <% = %>

The general difference is that the above two methods, then we will solve these two problems, so that our custom methods are the same as those of Microsoft.

1. Solve the problem first <% = %>

When you call the method provided by Microsoft, do you find the type of Return Value of the method defined by it?

The Return Value Type of our previous method is string, so next we will redefine a method whose return value type is mvchtmlstring. The Code is as follows:

 public static MvcHtmlString GetSpanMVC(this HtmlHelper htmlHelper, string text)        {            return MvcHtmlString.Create("<span style='color:Red'>"+text+"</span>");        }

Test the code on the following page:

The effect is as follows:

Here we have solved one of the differences between the custom method and Microsoft, and The namespace problem,

In fact, the namespace is easy to solve. Why does Microsoft not need to reference it? What is his namespace? Is our namespace the same as Microsoft's? After this series of problems came out, we began to look for Microsoft's namespace, and changed our custom namespace to the same as Microsoft's.

This will not be explained. You can look for it as system. web. MVC. html. After changing the namespace name of the previous class to this one, you can remove the reference on the page. Here, our custom methods are basically the same as those of Microsoft.

 

 

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.