How does an ASP. NET WebForm Form implement the "Automatic Assembly" effect of MVC?

Source: Internet
Author: User

How does an ASP. NET WebForm Form implement the "Automatic Assembly" effect of MVC?
We know ASP. net mvc has a powerful point: when the Form is submitted to the action, the Form parameters can be directly assembled into the parameter object of the action, such as the action Method Register (UserModel userModel) {.............................} when submitting a form, the fields in the form are automatically encapsulated into the corresponding UserModel field. Can the fields in the WebForm be changed? Because every time we get data, good programmers should learn code encapsulation, code reuse, and repetitive work. We can actually use reflection to instantiate objects (automatic assembly) beijing Alliance network www.bamn.cn is good. There is not much nonsense .... pageload is simple.

Protected void Page_Load (object sender, EventArgs e) {if (! IsPost () {InitPage (); // The first access to the rendering page} else {UserModel userModel = AssembleModel <UserModel> (base. valueCollection );}}

 

The key is the AssembleModel method in the base class. In the base class, we first obtain the context parameter IT404 protected NameValueCollection valueCollection = HttpContext. current. request. params; the base class is very simple. It stores the submitted parameters of the context to valueCollection and then looks at the AssembleModel method. This is a generic method.
/// <Summary> /// obtain the attributes of the class through reflection /// </summary> /// <param name = "type"> </param> // <returns> </returns> protected PropertyInfo [] GetPropertyInfoArray (Type type) {PropertyInfo [] props = null; try {object obj = Activator. createInstance (type); props = type. getProperties (BindingFlags. public | BindingFlags. instance);} catch (Exception ex) {} return props ;} /// <summary> /// automatically assemble according to NameValueCollection /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "valueCollection"> </param> // <returns> </returns> protected T AssembleModel <T> (NameValueCollection valueCollection) {PropertyInfo [] propertyInfoList = GetPropertyInfoArray (typeof (T); object obj = Activator. createInstance (typeof (T), null); // create a specified type of instance foreach (string key in valueCollection. keys) // value of all contexts {foreach (var PropertyInfo in propertyInfoList) // all object attributes {if (key. toLower () = PropertyInfo. name. toLower () {PropertyInfo. setValue (obj, valueCollection [key], null); // assign a value to the object }}return (T) obj ;}

 

It is easy to traverse the parameters, and then traverse the common attributes of the Object Class Using Reflection, then match and assign values based on the name. Therefore, in the future, we only need one code to automatically assemble the UserModel userModel = AssembleModel <UserModel> (base. valueCollection); well, thank you for reading this article. I hope it will help you. In addition, this blog will share some original video tutorials with you. Thank you for your support.

Related Article

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.