将form表单元素转为实体对象 或集合 -ASP.NET C#

简介:

做WEBFROM开发的同学都知道后台接收参数非常麻烦

虽然MVC中可以将表单直接转为集实,但不支持表单转为 LIST<T>这种集合

 

单个对象的用法:

表单:

 

1
2
<input name= 'id'   value= '1'  >
<input name= 'sex'   value= '男'  >

后台:

复制代码
            //以前写法
            DLC_category d = new DLC_category();
            d.sex = Request["sex"];
            d.id = Convert.ToInt32(Request["id"]);


            //现在写法
            var category = RequestToModel.GetSingleForm<DLC_category>();
复制代码

 

集合对象的用法:

表单:

1
2
3
4
5
6
7
8
9
< 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='女' >

  

后台:

  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>
     /// ** 描述:表单帮助类
     /// ** 创始时间:2015-4-17
     /// ** 修改时间:-
     /// ** 作者:sunkaixuan
     /// ** qq:610262374 欢迎交流,共同提高 ,命名语法等写的不好的地方欢迎大家的给出宝贵建议
     /// </summary>
     public  class  RequestToModel
     {
 
         /// <summary>
         /// 提交表单通过反射获取单个像
         /// 注意:表单控件name必包含对应类中的第一个字段,否则将报错
         /// </summary>
         public  static  T GetSingleForm<T>()  where  T :  new ()
         {
             T t = SetList<T>( null , 0).Single();
             return  t;
         }
 
 
         /// <summary>
         /// 提交表单通过反射获取单个像
         /// 注意:表单控件name必包含对应类中的第一个字段,否则将报错
         /// <param name="appstr">控件前缀,比如 name="form1.sex" appstr可以设为form1</param>
         /// </summary>
         public  static  T GetSingleForm<T>( string  appstr)  where  T :  new ()
         {
             T t = SetList<T>(appstr, 0).Single();
             return  t;
         }
 
         /// <summary>
         /// 提交表单通过反射获取多个对像
         /// 注意:表单控件name必包含对应类中的第一个字段,否则将报错
         /// </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>
         /// 提交表单通过反射获取多个对像
         /// 注意:表单控件name必包含对应类中的第一个字段,否则将报错
         /// </summary>
         /// <typeparam name="type"></typeparam>
         /// <param name="appstr">控件前缀,比如 name="form1.sex" appstr可以设为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>
         /// 提交表单通过反射获取多个对像
         /// </summary>
         /// <typeparam name="type"></typeparam>
         /// <param name="appstr">控件前缀,比如 name="form1.sex" appstr可以设为form1</param>
         /// <typeparam name="index">表单控件中第一个控件,对应类中字段在该类中的索引号,特殊情况可以是第二第三控件</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  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;
         }
     }
}

  

转载于:https://www.cnblogs.com/lsgsanxiao/p/7111064.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值