Brief introduction:
Do webfrom development of students know backstage receive parameters very troublesome
Although the form can be directly converted to set in MVC, the form is not supported to list<t> such a collection
Use of a single object:
Form:
Copy Code code as follows:
<input name= ' id ' value= ' 1 ' >
<input name= ' sex ' value= ' man ' >
Background:
Copy Code code as follows:
Previous writing
Dlc_category d = new Dlc_category ();
D.sex = request["Sex"];
D.id = Convert.ToInt32 (request["id"));
Now, the wording
var category = Requesttomodel.getsingleform<dlc_category> ();
Use of Collection objects:
Form:
Copy Code code as follows:
<input name= ' id ' value= ' 1 ' >
<input name= ' sex ' value= ' man ' >
<input name= ' id ' value= ' 2 ' >
<input name= ' sex ' value= ' female ' >
<input name= ' id ' value= ' 3 ' >
<input name= ' sex ' value= ' female ' >
Background:
Copy Code code as follows:
list<dlc_category> categorylists = requesttomodel.getlistbyform<dlc_category> ();
Source:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web; namespace Syntacticsugar {///<summary>///* * Description: Form Help///* * Founder Time: 2015-4-17///* * Modified:-///* * Author: Sun
KaiXuan///* * qq:610262374 welcome the Exchange, common improvement, naming grammar and other bad written places welcome everyone to give valuable suggestions///</summary> public class Requesttomodel { <summary>///Submit form by reflection to get a single like///Note: Form control name must contain the first field in the corresponding class, otherwise the error will be///</summary> public Static T getsingleform<t> () where t:new () {t = setlist<t> (NULL, 0).
Single ();
return t; ///<summary>///Submit form through reflection to get a single like///Note: Form control name must contain the first field in the corresponding class, otherwise the error///<param name= "Appstr" > control prefixes, such as name= "Form1.sex" Appstr can be set to form1</param>///</summary> public static T getsingleform<t > (string appstr) where t:new () {t = setlist<t> (appstr, 0).
Single ();
return t; ///<summary>///Submit form to get multiple pairs like///Note by reflection: Form ControlThe item name must contain the first field in the corresponding class, otherwise the error///</summary>///<typeparam name= "type" ></typeparam>///<para M 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 form to get multiple pairs like///by reflection Note: The form control name must contain the first field in the corresponding class, otherwise the error will be///</summary>/// <typeparam name= "type" ></typeparam>///<param name= "appstr" > control prefix, such as name= "Form1.sex" Appstr can be set to fo rm1</param>///<returns></returns> public static list<t> getlistbyform<t> (String AP
PSTR) where T:new () {list<t> T = setlist<t> (appstr, 0);
return t; ///<summary>///Submit form to get multiple pairs like///</summary>///<typeparam name= "type" via Reflection ></ty peparam>///<param name= "appstr" > control prefix, such as name= "Form1.sThe ex "APPSTR can be set to the first control in the form1</param>///<typeparam name=" index "> Form control, the index number of the field in the class in the corresponding class, and, in particular, the second third control </ typeparam>///<returns></returns> private static list<t> getlistbyform<t> (String appst
R, 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.N
Ame + ""]; 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);
The catch (Exception ex) {throw ex;
} return t;
}
}
}