簡(jiǎn)介:
做WEBFROM開(kāi)發(fā)的同學(xué)都知道后臺(tái)接收參數(shù)非常麻煩
雖然MVC中可以將表單直接轉(zhuǎn)為集實(shí),但不支持表單轉(zhuǎn)為 LIST<T>這種集合
單個(gè)對(duì)象的用法:
表單:
<input name='id' value='1' >
<input name='sex' value='男' >
后臺(tái):
//以前寫法
DLC_category d = new DLC_category();
d.sex = Request["sex"];
d.id = Convert.ToInt32(Request["id"]);
//現(xiàn)在寫法
var category = RequestToModel.GetSingleForm<DLC_category>();
集合對(duì)象的用法:
表單:
<input name='id' value='1' >
<input name='sex' value='男' >
<input name='id' value='2' >
<input name='sex' value='女' >
<input name='id' value='3' >
<input name='sex' value='女' >
后臺(tái):
List<DLC_category> categoryLists = RequestToModel.GetListByForm<DLC_category>();
源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace SyntacticSugar { /// <summary> /// ** 描述:表單幫助類 /// ** 創(chuàng)始時(shí)間:2015-4-17 /// ** 修改時(shí)間:- /// ** 作者:sunkaixuan /// ** qq:610262374 歡迎交流,共同提高 ,命名語(yǔ)法等寫的不好的地方歡迎大家的給出寶貴建議 /// </summary> public class RequestToModel { /// <summary> /// 提交表單通過(guò)反射獲取單個(gè)像 /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) /// </summary> public static T GetSingleForm<T>() where T : new () { T t = SetList<T>( null , 0).Single(); return t; } /// <summary> /// 提交表單通過(guò)反射獲取單個(gè)像 /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) /// <param name="appstr">控件前綴,比如 name="form1.sex" appstr可以設(shè)為form1</param> /// </summary> public static T GetSingleForm<T>( string appstr) where T : new () { T t = SetList<T>(appstr, 0).Single(); return t; } /// <summary> /// 提交表單通過(guò)反射獲取多個(gè)對(duì)像 /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) /// </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> /// 提交表單通過(guò)反射獲取多個(gè)對(duì)像 /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) /// </summary> /// <typeparam name="type"></typeparam> /// <param name="appstr">控件前綴,比如 name="form1.sex" appstr可以設(shè)為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> /// 提交表單通過(guò)反射獲取多個(gè)對(duì)像 /// </summary> /// <typeparam name="type"></typeparam> /// <param name="appstr">控件前綴,比如 name="form1.sex" appstr可以設(shè)為form1</param> /// <typeparam name="index">表單控件中第一個(gè)控件,對(duì)應(yīng)類中字段在該類中的索引號(hào),特殊情況可以是第二第三控件</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; } } } |