国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

ASP.NET中實(shí)現(xiàn)把form表單元素轉(zhuǎn)為實(shí)體對(duì)象或集合

2024-07-10 13:29:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)把form表單元素轉(zhuǎn)為實(shí)體對(duì)象或集合,本文又是一個(gè)對(duì)重復(fù)數(shù)據(jù)處理的一個(gè)封裝,非常實(shí)用的開發(fā)技巧,需要的朋友可以參考下
 

簡(jiǎn)介:

做WEBFROM開發(fā)的同學(xué)都知道后臺(tái)接收參數(shù)非常麻煩

雖然MVC中可以將表單直接轉(zhuǎn)為集實(shí),但不支持表單轉(zhuǎn)為 LIST<T>這種集合

單個(gè)對(duì)象的用法:

表單:

 

復(fù)制代碼代碼如下:

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

 

后臺(tái):

復(fù)制代碼代碼如下:

//以前寫法
            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ì)象的用法:

表單:

復(fù)制代碼代碼如下:

<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):
復(fù)制代碼代碼如下:

  List<DLC_category> categoryLists = RequestToModel.GetListByForm<DLC_category>();

 

源碼:

 

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5.   
  6. namespace SyntacticSugar 
  7.   /// <summary> 
  8.   /// ** 描述:表單幫助類 
  9.   /// ** 創(chuàng)始時(shí)間:2015-4-17 
  10.   /// ** 修改時(shí)間:- 
  11.   /// ** 作者:sunkaixuan 
  12.   /// ** qq:610262374 歡迎交流,共同提高 ,命名語(yǔ)法等寫的不好的地方歡迎大家的給出寶貴建議 
  13.   /// </summary> 
  14.   public class RequestToModel 
  15.   { 
  16.   
  17.     /// <summary> 
  18.     /// 提交表單通過(guò)反射獲取單個(gè)像 
  19.     /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) 
  20.     /// </summary> 
  21.     public static T GetSingleForm<T>() where T : new() 
  22.     { 
  23.       T t = SetList<T>(null, 0).Single(); 
  24.       return t; 
  25.     } 
  26.   
  27.   
  28.     /// <summary> 
  29.     /// 提交表單通過(guò)反射獲取單個(gè)像 
  30.     /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) 
  31.     /// <param name="appstr">控件前綴,比如 name="form1.sex" appstr可以設(shè)為form1</param> 
  32.     /// </summary> 
  33.     public static T GetSingleForm<T>(string appstr) where T : new() 
  34.     { 
  35.       T t = SetList<T>(appstr, 0).Single(); 
  36.       return t; 
  37.     } 
  38.   
  39.     /// <summary> 
  40.     /// 提交表單通過(guò)反射獲取多個(gè)對(duì)像 
  41.     /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) 
  42.     /// </summary> 
  43.     /// <typeparam name="type"></typeparam> 
  44.     /// <param name="type"></param> 
  45.     /// <returns></returns> 
  46.     public static List<T> GetListByForm<T>() where T : new() 
  47.     { 
  48.       List<T> t = SetList<T>(null, 0); 
  49.       return t; 
  50.     } 
  51.   
  52.     /// <summary> 
  53.     /// 提交表單通過(guò)反射獲取多個(gè)對(duì)像 
  54.     /// 注意:表單控件name必包含對(duì)應(yīng)類中的第一個(gè)字段,否則將報(bào)錯(cuò) 
  55.     /// </summary> 
  56.     /// <typeparam name="type"></typeparam> 
  57.     /// <param name="appstr">控件前綴,比如 name="form1.sex" appstr可以設(shè)為form1</param> 
  58.     /// <returns></returns> 
  59.     public static List<T> GetListByForm<T>(string appstr) where T : new() 
  60.     { 
  61.       List<T> t = SetList<T>(appstr, 0); 
  62.       return t; 
  63.     } 
  64.   
  65.   
  66.     /// <summary> 
  67.     /// 提交表單通過(guò)反射獲取多個(gè)對(duì)像 
  68.     /// </summary> 
  69.     /// <typeparam name="type"></typeparam> 
  70.     /// <param name="appstr">控件前綴,比如 name="form1.sex" appstr可以設(shè)為form1</param> 
  71.     /// <typeparam name="index">表單控件中第一個(gè)控件,對(duì)應(yīng)類中字段在該類中的索引號(hào),特殊情況可以是第二第三控件</typeparam> 
  72.     /// <returns></returns> 
  73.     private static List<T> GetListByForm<T>(string appstr, int index) where T : new() 
  74.     { 
  75.       List<T> t = SetList<T>(appstr, index); 
  76.       return t; 
  77.     } 
  78.   
  79.   
  80.   
  81.     private static List<T> SetList<T>(string appendstr, int index) where T : new() 
  82.     { 
  83.       List<T> t = new List<T>(); 
  84.       try 
  85.       { 
  86.         var properties = new T().GetType().GetProperties(); 
  87.         var subNum = System.Web.HttpContext.Current.Request[appendstr + properties[index].Name].Split(',').Length; 
  88.         for (int i = 0; i < subNum; i++) 
  89.         { 
  90.           var r = properties; 
  91.           var model = new T(); 
  92.           foreach (var p in properties) 
  93.           { 
  94.             string pval = System.Web.HttpContext.Current.Request[appendstr + p.Name + ""]; 
  95.             if (!string.IsNullOrEmpty(pval)) 
  96.             { 
  97.               pval = pval.Split(',')[i]; 
  98.               string pptypeName = p.PropertyType.Name; 
  99.               p.SetValue(model, Convert.ChangeType(pval, p.PropertyType), null); 
  100.             } 
  101.           } 
  102.           t.Add(model); 
  103.         } 
  104.       } 
  105.       catch (Exception ex) 
  106.       { 
  107.   
  108.   
  109.         throw ex; 
  110.       } 
  111.   
  112.   
  113.       return t; 
  114.     } 
  115.   } 
?
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 司法| 右玉县| 泸定县| 德令哈市| 广东省| 于都县| 河池市| 河北省| 康乐县| 绥化市| 湟源县| 保德县| 嵊泗县| 靖宇县| 临夏县| 新津县| 兰溪市| 洪湖市| 五指山市| 合水县| 萨迦县| 湖北省| 丰镇市| 日土县| 新建县| 冕宁县| 嘉祥县| 大邑县| 周宁县| 沈丘县| 绥江县| 阿拉善盟| 卢氏县| 沂源县| 双辽市| 积石山| 垦利县| 洛宁县| 红河县| 丰原市| 横山县|