Convert form elements into object objects or sets. form forms
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:
<Input name = 'id' value = '1'> <input name = 'sex' value = 'male'>
Background:
// DLC_category d = new DLC_category (); d. sex = Request ["sex"]; d. id = Convert. toInt32 (Request ["id"]); // var category = RequestToModel. getSingleForm <DLC_category> ();
Collection object usage:
Form:
<Input name = 'id' value = '1'> <input name = 'sex' value = 'male'> <input name = 'id' value = '2'> <input name = 'sex' value = 'femal'> <input name = 'id' value = '3'> <input name = 'sex' value = 'femal'>
Background:
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 GetSingle Form <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 application 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.s Ex "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 ;}}}