Today, let's talk about ASP. net mvc without talking about fluent nhib.pdf. In MVC views, we often use htmlhelper to generate various htmlCode(You may not be clear about the code ).
Htmlhelper does not have many functions. Fortunately, there are many extension methods. We can use it to generate an input control, for example:
<%=Html. Textbox ("Userid")%>
We can use the above Code to generate a single line text box without values. We can also use:
<%=Html. Textbox ("Userid", Model. User. userid,New{@ Class ="Class"})%>
Add a style to this text box and the corresponding HTML code:
<InputType= "Text"ID= "Userid"Name= "Userid"Class= "Class"Value="<%= Model. User. userid%>"/>
It is really convenient. With the extension method, we can easily create various controls, including form. But what if you want to generate many controls? Let's just give an example. For example, if you want to add a product, it may have dozens of attributes, which means you need to write dozens of HTML. textbox (attribute value) (Other controls can be used here) is actually nothing, but it is really uncomfortable. Maybe I am used to the ing method of fluent nhib.pdf, I keep thinking about using the following methods to generate controls:
<%=Html. textbox <User> (U => U. userid, model. User. userid,New{@ Class ="Class"})%>
How about it? Isn't the code very beautiful? It's not hard-coded, but it looks nice? haha, but htmlhelper doesn't have this extension method, so we can only write an extension by ourselves.
I have to thank Microsoft for providing such a good extension method. With it, everything becomes simple. I saw a friend asking if the project should be upgraded to framework3.5, I think it is very necessary, because with it, the code looks so beautiful.
Because I was just trying to access MVC, I looked at it.Source code, The extension methods of htmlhelper are all in system. web. MVC. in the HTML namespace, I roughly read the inputextensions Code. It is a simple method to generate an input control based on the input type and name, but it provides us with great convenience. However, it is worth noting that if your Textbox (name) contains ".", your Control ID will replace ".".
It is not difficult to do what you do. It is actually to parse an Expression Tree and retrieve the name of its attribute. Introduction to the lambda Expression TreeArticleA lot. I will not introduce it here.
Public static string Getmemebername <t> ( This Expression <T> Expression ){ Memberexpression Memberexpression = Null ; If (Expression. Body. nodetype = Expressiontype . Convert ){ VaR Body = ( Unaryexpression ) Expression. Body; memberexpression = body. operand As Memberexpression ;} Else if (Expression. Body. nodetype = Expressiontype . Memberaccess) {memberexpression = expression. Body As Memberexpression ;} Else { Throw new Argumentexception ( "Expression argument is error" );} Return Memberexpression. member. Name ;}
Here, membername is used to obtain the Expression Tree. With it, we can implement the original idea:
Public static string Textbox <t> ( This Htmlhelper Helper, Expression < Func <T, Object > Expression ){ Return Helper. textbox <t> (expression, Null );} Public static string Textbox <t> ( This Htmlhelper Helper, Expression < Func <T, Object > Expression,Object Value ){ Return Helper. textbox <t> (expression, value, Null );} Public static string Textbox <t> ( This Htmlhelper Helper, Expression < Func <T, Object > Expression, Object Value, Object Htmlattributes ){ Return Helper. Textbox (expression. getmemebername (), value, htmlattributes );}
OK. We can use the following method to construct a textbox:
< Table > < Tr > < TD Colspan = "2"> User Login </ TD > </ Tr > < Tr > < TD > User name: </ TD > < TD > <% = Html. textbox <studymvcapplication. models. User > (U => U. Name) %> </ TD > </ Tr > < Tr > < TD > Password </ TD > < TD > <% = Html. textbox <studymvcapplication. models. User > (U => U. Password) %> </ TD > </ Tr > </ Table >
It looks much more comfortable. Haha, Look at it:
Summary
I haven't continued the test yet. It's a little busy. Some people may say it's a bit redundant. I like it, haha. However, htmlhelper has many extension methods, so you still need to write a lot of code. I will add them.
To be honest, it seems that MVC is hard-coded in many places, so it is not easy to transform a suitable environment. Old Zhao recently obtained an mvcpath. Do you want to put it together? Roar
This article demonstrates how to download the code