. NET WebForm simulates MVC for model binding, making yourself less overworked

Source: Internet
Author: User

The brothers who used MVC know that MVC has the data function of model binding form submission, then I also want to steal a lazy also write a webform version of the model binding. This defines a generic method, and then assigns the value of the corresponding property name on the form to the property of the reflection-created class by reflection.

Places to be noticed:

1. The attribute name of the model class that is defined corresponds to the name of the form name

2, the definition of the generic method is through the var form = context.   Request.Form; Data from the form post

 Public classdatamodel{/// <summary>    ///binding the values of the model models from the data submitted by the form/// </summary>    /// <typeparam name= "T" ></typeparam>    /// <param name= "entity" ></param>    /// <returns></returns>     Public StaticT modelbing<t>(T entity) {Objectobj = Activator.CreateInstance (typeof(T)); Type type=entity.        GetType (); Propertyinfo[] Proinfos=type.        GetProperties (); Dictionary<string, propertyinfo> dic =Newsystem.collections.generic.dictionary<string, propertyinfo>();  for(inti =0; i < proinfos.length; i++) {dic. ADD (Proinfos[i].        Name, Proinfos[i]); } HttpContext context=HttpContext.Current; varform =context.        Request.Form; string[] keys =form.        AllKeys; foreach(stringKeyinchkeys) {            if(DIC. ContainsKey (key)) {PropertyInfo PInfo=Dic[key]; varProtype =Pinfo.propertytype; ObjectValue =NULL;//Store conversion value after the corresponding type                if(string. IsNullOrEmpty (Form[key]))//when the value of post comes down                {                    if(Protype = =typeof(string))//determines whether the property is a string type to an empty string because my database does not allow null for this field{//Here I do not judge the value type's data, because the value type will have the default valuePinfo.setvalue (obj,"",NULL); }                                                           Continue; }                Try                {                    if(Protype.isenum)//If the property is an enumeration type, the conversion enumeration type{Value=Enum.toobject (Protype, Form[key]); }                    Else if(Protype.isgenerictype && ProType.Name.StartsWith ("Nullable"))//generic type{Type Newprotype=Nullable.getunderlyingtype (Protype); Value=Convert.changetype (Form[key], newprotype); }                    Else{Value=Convert.changetype (Form[key], protype); } pinfo.setvalue (obj, value,NULL); }                Catch(Exception ex) {ThrowEx//Write error log                }            }        }        return(T) obj; }}

The front desk can be validated through the Easyui framework of the form and then submitted to the backstage, here will no longer say, we understand how to use this method is good, there is not perfect place to hope you point out!

. NET WebForm simulates MVC for model binding, making yourself less overworked

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.