ASP. NET converts form elements into Entity objects or collections. asp. netform

Source: Internet
Author: User

ASP. NET converts form elements into Entity objects or collections. asp. netform

Introduction:

WEBFROM developers know that it is very troublesome to receive parameters in the background.

Although the form in MVC can be directly converted into a set, the form conversion to a LIST <T> is not supported.

Usage of a single object:

Form:

Copy codeThe Code is as follows:
<Input name = 'id' value = '1'>
<Input name = 'sex' value = 'male'>

Background:
Copy codeThe Code is as follows:
// Previous statement
DLC_category d = new DLC_category ();
D. sex = Request ["sex"];
D. id = Convert. ToInt32 (Request ["id"]);


// Write now
Var category = RequestToModel. GetSingleForm <DLC_category> ();

Collection object usage:

Form:
Copy codeThe Code is as follows:
<Input name = 'id' value = '1'>
<Input name = 'sex' value = 'male'>
 
 
<Input name = 'id' value = '2'>
<Input name = 'sex' value = 'female '>
 
<Input name = 'id' value = '3'>
<Input name = 'sex' value = 'female '>

Background:
Copy codeThe Code is as follows:
List <DLC_category> categoryLists = RequestToModel. GetListByForm <DLC_category> ();

Source code:

Using System; using System. collections. generic; using System. linq; using System. web; namespace SyntacticSugar {// <summary> /// ** Description: Form help class // ** Creation Time: // ** modification time: -/// ** Author: sunkaixuan // ** qq: 610262374. Welcome to join us for further improvement, naming syntax and other poorly written information. You are welcome to give valuable suggestions. /// </summary> public class RequestToModel {// <summary> // submit a form to obtain a single object through reflection image // note: the Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned. // </summary> public static T GetSingl EForm <T> () where T: new () {T = SetList <t> (null, 0 ). single (); return t;} // <summary> // submit a form to obtain a Single image through reflection // note: the Form Control name must contain the first field in the corresponding class. Otherwise, an error is reported. // <param name = "appstr"> Control prefix, for example, name = "form1.sex" appstr can be set to form1 </param> // </summary> public static T GetSingleForm <T> (string appstr) where T: new () {T t = SetList <T> (appstr, 0 ). single (); return t;} // <summary> // submit a form to obtain multiple objects through reflection // Note: The Form Control name must contain The first field in the corresponding class, otherwise, an error is returned. // </summary> /// <typeparam name = "type"> </typeparam> /// <param name = "type"> </param> /// <returns> </returns> public static List <T> GetListByForm <T> () where T: new () {List <T> t = SetList <T> (null, 0); return t ;} /// <summary> /// submit a form to obtain multiple objects through reflection // Note: The Form Control name must contain the first field in the corresponding class, otherwise, an error is reported: // </summary> /// <typeparam name = "type"> </typeparam> /// <param name = "appstr"> Control prefix, for example, name = "form1. Sex "appstr can be set to form1 </param> // <returns> </returns> public static List <T> GetListByForm <T> (string appstr) where T: new () {List <T> t = SetList <T> (appstr, 0); return t ;} /// <summary> /// submit a form to obtain multiple objects through reflection /// </summary> /// <typeparam name = "type"> </typeparam> /// <param name = "appstr"> Control prefix, for example, name = "form1.sex" appstr can be set to form1 </param> // The first control in the <typeparam name = "index"> form control, the index number of the field in the corresponding class in this class. In special cases, it can be the second and third control. </Typeparam> /// <returns> </returns> private static List <T> GetListByForm <T> (string appstr, int index) where T: new () {List <T> t = SetList <T> (appstr, index); return t;} private static List <T> SetList <T> (string appendstr, int index) where T: new () {List <T> t = new List <T> (); try {var properties = new T (). getType (). getProperties (); var subNum = System. web. httpContext. current. request [appendstr + Properties [index]. name]. split (','). length; for (int I = 0; I <subNum; I ++) {var r = properties; var model = new T (); foreach (var p in properties) {string pval = System. web. httpContext. current. request [appendstr + p. name + ""]; if (! String. isNullOrEmpty (pval) {pval = pval. split (',') [I]; string pptypeName = p. propertyType. name; p. setValue (model, Convert. changeType (pval, p. propertyType), null) ;}} t. add (model) ;}} catch (Exception ex) {throw ex;} return t ;}}}

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.