From http://www.cnblogs.com/think8848/archive/2011/09/14/2175432.html
Reprinted please indicate the author (think8848) and source (http://think8848.cnblogs.com)
According to my practice, I would like to start with something unrelated to the topic: I was going to stick to this habit of writing blogs, and even if I could not produce high-quality goods, I could also produce some goods, provides some solutions for some small problems,
However, in August of this year, it was a "Autumn" of many things. Many things were brought together and there was almost no time to learn new things, let alone to write a blog, in September, it was about half of the time. It was just yesterday that a small East was born.
West, I think it's a little tricky.
I plan to use ASP. net MVC implemented a product of the company. Yesterday I encountered a problem: when an exception occurred, I switched back to the page before submission and the original input content disappeared, which is a big problem, remember that I was writing ASP. net MVC Exception Handling solution has solved this problem. Why can't I see the input before submission?CodeThe problem is found:
In the current Code, the form code is:
<Input ID="Txtname" Name="Name" Type="Text" /> |
The form code that can see the effect before is:
@ Html. textboxfor (x => X. Name) |
After a test, we found that the <input> tag generated by using the HTML extension method can obtain the value before submission, but it cannot be manually written.
There is definitely a mechanism in the textbox Extension Method to automatically enter the value in the <input> label. After discussing with colleagues, I thought that
@ Html. textboxfor has a benefit, that is, if the name of the name attribute is changed, VS can automatically refactor the code to make X. Name of *. cshtml code
Updated to the new property name. After testing, we found that this was not the case. We modified the name of the name attribute, such as pname, and used vs to reconstruct the code, we found that the attribute name has not been changed in the view.
And if you use HTML extension methods, there may also be some problems. The most important thing is not intuitive. It is not obvious in the current razor engine, and there is no designer anyway, however, if
The razor engine has the designer function. It can be concluded that the @ html. textboxfor () method is hard to achieve what you see is what you get.
On the cshtml page, the designer is not immediately available. It is not intuitive to use @ html. XXX in the view when you look at the code. Since the HTML extension method cannot be used for reconstruction
The reason for using HTML tags seems to be much more intuitive. If you use this method, even if you do not use C #, you can write the page.
Driven by this idea, we can come up with a way to expand the label value by ourselves. Open ASP. NET MVC
3Source codeTo see what is being done inside the textbox, why can it set the values in the model (viewdata. Model) and
The value in viewdata. modelstate is filled in the tag and checked step by step. It is found that the original implementation method is relatively simple and the code is directly listed:
Public Static Class Htmlvalueextension |
Public Static Mvchtmlstring value <tModel, tproperty> (This Htmlhelper <tModel> HTML, expression <func <tModel, tproperty> expression) |
Modelmetadata metadata = modelmetadata. fromlambdaexpression (expression, HTML. viewdata ); |
Return Value (HTML, metadata. propertyname ); |
Public Static Mvchtmlstring value (This Htmlhelper HTML,String Name) |
String Attemptedvalue =Null; |
If (Html. viewdata. modelstate. trygetvalue (name,Out Modelstate )) |
If (Modelstate. value! =Null) |
Attemptedvalue = modelstate. value. convertize (Typeof(String),Null /* Culture */). Tostring (); |
Return New Mvchtmlstring (attemptedvalue ?? Convert. tostring (html. viewdata. eval (name), cultureinfo. currentculture )); |
Define an extension method value <tModel, tproperty> of htmlhelper <tModel>, and then
Lambda expressions obtain the metadata of a specified attribute. The corresponding data is obtained from modelstate, that is, the form data before submission. If the data is null
Try the data specified in viewdata. model. It's easy :)
With this class, you can use the following code to call it on the page:
<Input ID="Txtdeptname" Name="Name" Type="Text" Value = "@ html. Value (x => X. Name)"/> |
In this way, it can be reached with @ HTML. textbox () has the same effect, but from the perspective of view code, it is a lot more intuitive, and if the razor engine has a designer in the future, it is estimated that the page effect can be seen without debugging.
Finally, if you define a form tag in a razor view, the value of this form tag does not correspond to an attribute of the model, if you want to obtain the value before submission, use request. params ["tagname.
Reprinted please indicate the author (think8848) and source (http://think8848.cnblogs.com)
According to my practice, I would like to start with something unrelated to the topic: I was going to stick to this habit of writing blogs, and even if I could not produce high-quality goods, I could also produce some goods, provides some solutions for some small problems,
However, in August of this year, it was a "Autumn" of many things. Many things were brought together and there was almost no time to learn new things, let alone to write a blog, in September, it was about half of the time. It was just yesterday that a small East was born.
West, I think it's a little tricky.
I plan to use ASP. net MVC implemented a product of the company. Yesterday I encountered a problem: when an exception occurred, I switched back to the page before submission and the original input content disappeared, which is a big problem, remember that I was writing ASP. net MVC Exception Handling solution has solved this problem. Why can't I see the input before submission? I opened the previous Code and found the problem:
In the current Code, the form code is:
<Input ID="Txtname" Name="Name" Type="Text" /> |
The form code that can see the effect before is:
@ Html. textboxfor (x => X. Name) |
After a test, we found that the <input> tag generated by using the HTML extension method can obtain the value before submission, but it cannot be manually written.
There is definitely a mechanism in the textbox Extension Method to automatically enter the value in the <input> label. After discussing with colleagues, I thought that
@ Html. textboxfor has a benefit, that is, if the name of the name attribute is changed, VS can automatically refactor the code to make X. Name of *. cshtml code
Updated to the new property name. After testing, we found that this was not the case. We modified the name of the name attribute, such as pname, and used vs to reconstruct the code, we found that the attribute name has not been changed in the view.
And if you use HTML extension methods, there may also be some problems. The most important thing is not intuitive. It is not obvious in the current razor engine, and there is no designer anyway, however, if
The razor engine has the designer function. It can be concluded that the @ html. textboxfor () method is hard to achieve what you see is what you get.
On the cshtml page, the designer is not immediately available. It is not intuitive to use @ html. XXX in the view when you look at the code. Since the HTML extension method cannot be used for reconstruction
The reason for using HTML tags seems to be much more intuitive. If you use this method, even if you do not use C #, you can write the page.
Driven by this idea, we can come up with a way to expand the label value by ourselves. Open ASP. NET MVC
3. Source Code. Let's see what is being done inside the textbox. why can it set the values in the model (viewdata. Model) and
The value in viewdata. modelstate is filled in the tag and checked step by step. It is found that the original implementation method is relatively simple and the code is directly listed:
Public Static Class Htmlvalueextension |
Public Static Mvchtmlstring value <tModel, tproperty> (This Htmlhelper <tModel> HTML, expression <func <tModel, tproperty> expression) |
Modelmetadata metadata = modelmetadata. fromlambdaexpression (expression, HTML. viewdata ); |
Return Value (HTML, metadata. propertyname ); |
Public Static Mvchtmlstring value (This Htmlhelper HTML,String Name) |
String Attemptedvalue =Null; |
If (Html. viewdata. modelstate. trygetvalue (name,Out Modelstate )) |
If (Modelstate. value! =Null) |
Attemptedvalue = modelstate. value. convertize (Typeof(String),Null /* Culture */). Tostring (); |
Return New Mvchtmlstring (attemptedvalue ?? Convert. tostring (html. viewdata. eval (name), cultureinfo. currentculture )); |
Define an extension method value <tModel, tproperty> of htmlhelper <tModel>, and then
Lambda expressions obtain the metadata of a specified attribute. The corresponding data is obtained from modelstate, that is, the form data before submission. If the data is null
Try the data specified in viewdata. model. It's easy :)
With this class, you can use the following code to call it on the page:
<Input ID="Txtdeptname" Name="Name" Type="Text" Value = "@ html. Value (x => X. Name)"/> |
In this way, it can be reached with @ HTML. textbox () has the same effect, but from the perspective of view code, it is a lot more intuitive, and if the razor engine has a designer in the future, it is estimated that the page effect can be seen without debugging.
Finally, if you define a form tag in a razor view, the value of this form tag does not correspond to an attribute of the model, if you want to obtain the value before submission, use request. params ["tagname.