Asp. NET to implement form form forms field values automatically fill in the Operation model _ practical skills

Source: Internet
Author: User

We know that asp.net mvc has a powerful place to be when form forms are submitted to an action, you can directly assemble the form's parameters directly into the action's parameter entity object

Like what

Copy Code code as follows:

Action Method Register (Usermodel Usermodel)

{

.............................

}


When submitting a form, the fields inside the form are automatically encapsulated into the corresponding Usermodel field

So WebForm inside can also purple will it?

Because every time to get data, good programmers should learn code encapsulation, code reuse, repetitive work do not do

We can actually use reflection to instantiate objects (automatic assembly)

Well, there's not much nonsense ....

Pageload, it's easy inside.

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (! IsPost ())
{
Initpage ()//First Access Rendering page
}
Else
{
Usermodel Usermodel = assemblemodel<usermodel> (base.valuecollection);
}
}

The key is the Assemblemodel method inside the base class.

Base class Inside

We first get the parameters of the context IT404

Copy Code code as follows:

protected NameValueCollection valuecollection = HttpContext.Current.Request.Params;

The base class is simply to store the submitted parameters of the context in the ValueCollection

And then look at the Assemblemodel method, which is a generic method

Copy Code code as follows:

<summary>
Reflection gets the properties of a class
</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>
Automatic assembly based on 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 instance of specified type
foreach (string key in Valuecollection.keys)//value of all contexts
{
foreach (Var propertyinfo in propertyinfolist)//All Entity properties
{
if (key. ToLower () = = PropertyInfo.Name.ToLower ())
{
Propertyinfo.setvalue (obj, Valuecollection[key], NULL);//Assign value to object
}
}
}
Return (T) obj;
}

It's very simple to iterate through the parameters and then use reflection to traverse out the common properties of the entity class and then match and assign the values based on the name names.

So in the future, we just need a code to automatically assemble the values that are saved from the client.

Copy Code code as follows:

Usermodel Usermodel = assemblemodel<usermodel> (base.valuecollection);

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.