ASP. NET from form to entity class

Source: Internet
Author: User
Tags httpcontext

//new version, can be globa.ascx inside set development mode//to facilitate debugging errors//form to entity object version V2 Public classHttprequesthelper {/// <summary>        ///development mode, set to development mode, throw an exception, see the properties of the error, and type/// </summary>         Public Static BOOLDevMode {Get;Set; } #regionForm conversion to solid model/// <summary>        ///form conversion to solid model///usage: 1, INSERT, first new object incoming;///2, update, first isolated from the database this object incoming/// </summary>        /// <param name= "obj" >Generic Object</param>        /// <returns></returns>         Public StaticT convertform2model<t>(T obj) {if(NULL==obj) {obj=default(T); Obj= activator.createinstance<t>(); } Type Type=typeof(T); foreach(varPinchtype. GetProperties ()) {stringresult =Httpcontext.current.request.params[p.name]; if(!string. Isnullorwhitespace (Result)) {Try{p.setvalue (obj, Convert.changetype (result, P.propertytype),NULL); }                    Catch(Exception ex) {if(DevMode) {Throw NewException ("type conversion error, variable:"+ P.name +", type:"+ P.propertytype +", error message:"+Ex.                       Message); }                       Else                       {                           //logging ....                        }                   }                }            }            returnobj; }        #endregion    }

  /////////////////////do you have any good ideas, please give a suggestion//[email protected]////////////////// Public classHttprequesthelper<t>whereT:class,New()    {        #regionForm conversion to solid model/// <summary>        ///form conversion to solid model///usage: 1, INSERT, first new object incoming;///2, update, first isolated from the database this object incoming/// </summary>        /// <param name= "obj" >Generic Object</param>        /// <returns></returns>         Public StaticT Convertform2model (t obj=NULL)        {            if(NULL==obj) {obj=default(T); Obj= activator.createinstance<t>(); } Type Type=typeof(T); foreach(varPinchtype. GetProperties ()) {stringresult =Httpcontext.current.request.params[p.name]; if(!string. Isnullorwhitespace (Result)) {Try{p.setvalue (obj, Convert.changetype (result, P.propertytype),NULL); }                    Catch(Exception ex) {Throw NewException ("type conversion error, variable:"+ P.name +", type:"+ p.propertytype+", error message:"+Ex.                   Message); }                }            }            returnobj; }        #endregion    }

often encountered int age=convert.toint32 (THIS.TXTAGE.TEXT);

This kind of egg-ache code, close-up method.

The exception is thrown in the hope of knowing that the conversion failed, what the malformed property is, and that it is easy to debug.

ASP. From form to entity class

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.