As described in the question, cleverly use the priority of the Extension Method to beautify the textboxfor text box on your website (ASP. net mvc) page. With JS scripts, you can also restrict user input. And you don't even need to modify any front-end page...
First, let's look at the comparison chart before and after use:
Is it more beautiful on the right? Using a few simple extension methods, you can change the page of your entire ASP. net mvc website to the right.
The textboxfor extension method is ASP. net MVC 2, one of the strong type HTML helpers. If you are not familiar with it, read the scottgu's article ASP. net MVC 2: stronugly typed HTML helpers.
Let's step into the question and see how to implement it. Let's define several extension methods named textboxfor for the htmlhelper class. I don't have crazy! The name must be textboxfor. As follows:
Using system. LINQ. expressions;
Using system. Web. MVC;
Using system. Web. MVC. html;
Namespace mvc_textboxfor.htmlhelpers
{
Public static class textboxforextension
{
Public static mvchtmlstring textboxfor <tModel> (this htmlhelper <tModel> htmlhelper, expression <func <tModel, uint> expression)
{
Return htmlhelper. textboxfor (expression, new {@ class = "uint "});
}
Public static mvchtmlstring textboxfor <tModel> (this htmlhelper <tModel> htmlhelper, expression <func <tModel, datetime> expression)
{
Return htmlhelper. textboxfor (expression, new {@ class = "date "});
}
Public static mvchtmlstring textboxfor <tModel> (this htmlhelper <tModel> htmlhelper, expression <func <tModel, string> expression)
{
Return htmlhelper. textboxfor (expression, new {@ class = "string "});
}
Public static mvchtmlstring textboxfor <tModel> (this htmlhelper <tModel> htmlhelper, expression <func <tModel, decimal> expression)
{
Return htmlhelper. textboxfor (expression, new {@ class = "RMB "});
}
}
}
Will it conflict with the original textboxfor in MVC2? Of course not! The textboxfor extension in MVC2 is defined as follows:
Public static mvchtmlstring textboxfor <tModel, tproperty> (this htmlhelper <tModel> htmlhelper, expression <func <tModel, tproperty> expression)
{...}
The original textboxfor extension has two generic parameters. The four parameters I defined above have only one generic parameter.
There will be no name conflicts, but what will be called during execution? This involves the priority of extension method calls (this issue has been discussed in a specific article). In terms of generic extension methods, the more "generic" (there are many generic parameters but they are not clear), the lower the priority is.
In MVC2, there are more textboxfor generic parameters than I have defined, so the priority is lower. Therefore, the meaning of HTML. textboxfor on the page has changed. Instead of calling the original extension method, it calls several of my new definitions. Of course, the premise is to reference the namespace of the class where the extension method I defined is located (needless to say, the corresponding namespace must be referenced if you want to use extension ).
The simplest way to reference a namespace is to reference it in Web. config, as follows:
<Configuration>
<System. Web>
<Pages>
<Namespaces>
<Add namespace = "mvc_textboxfor.htmlhelpers"/>
</Namespaces>
</Pages>
</System. Web>
</Configuration>
I have defined several simple extension methods. I called the original textboxfor method in MVC2, but added different CSS styles based on different types.
. Uint,. Date,. RMB,. String {
Padding-top: 3px;
Height: 16px;
Padding-left: 22px;
Background-repeat: No-Repeat;
Background-position: 2px center;
}
. Uint {background-image: URL (/images/uint.png );}
. Date {background-image: URL (/images/date.png );}
. RMB {background-image: URL (/images/RMB .png );}
. String {background-image: URL (/images/string.png );}
Combined with the JS script, it can also restrict user input.
Implementation is so simple! It's late, leaving code for your reference: Click to download (70kb)
The clever use of the priority of the extension method will bring us unexpected results and hope to help you.
Please stay tuned to my series of articles "C # great use of extension methods!