简介:

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

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

单个对象的用法:

表单:

<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>();

集合对象的用法:

表单:

<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>();

源码:

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 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;
}
}
}

  

将form表单元素转为实体对象 或集合 -ASP.NET C#的更多相关文章

  1. HTTP上下文表单内容转为实体对象

    using ServiceStack.Web; using System; using System.Collections.Generic; using System.Linq; using Sys ...

  2. 【jQuery】form表单元素序列化为json对象

    序列化form表单元素为json对象: <!Doctype html> <html xmlns=http://www.w3.org/1999/xhtml> <head&g ...

  3. form表单元素的值序列化成对象

    /** * 将form表单元素的值序列化成对象 * param: form jquery form对象 */ var serializeObject = function(form) { var o ...

  4. 将form表单元素的值序列化成对象

    /**jQuery * 将form表单元素的值序列化成对象 * @returns object */ var serializeObject = function(form) { var o = {} ...

  5. 拓展jQuery的serialize(),将form表单转化为json对象

    jQuery 的 serialize() 方法经常会报 Uncaught TypeError: JSON.serializeObject is not a function 的错误, 原装的方法真的一 ...

  6. form表单元素设置只读

      form表单元素设置只读 CreateTime--2017年5月5日11:42:41 Author:Marydon 1.设置文本框只读 <!-- 方法一:简写 --> <inpu ...

  7. Form表单元素

    Form表单元素 action method input: name value type: text password button radio checkbox file submit reset ...

  8. jQuery操作Form表单元素

    Web开发中常常须要操作表单,form表单元素有select.checkbox.radio.textarea.button.file.text.hidden.password等. 当中checkbox ...

  9. form表单序列化为Jquery对象

    <form id="DailyFinancial" > @*class="form-inline"*@ <div class="fo ...

随机推荐

  1. Android UI系列-----时间、日期、Toasts和进度条Dialog

    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...

  2. 奇怪吸引子---WimolBanlue

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  3. liunx 套接字编程(Linux_C++)

    网络中的进程是如何通信的? 在网络中进程之间进行通信的时候,那么每个通信的进程必须知道它要和哪个计算机上的哪个进程通信.否则通信无从谈起!在本地可以通过进程PID来唯一标识一个进程,但是在网络中这是行 ...

  4. Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题

    A. Hongcow Learns the Cyclic Shift 题目连接: http://codeforces.com/contest/745/problem/A Description Hon ...

  5. 每日英语:The Secret About Online Ad Traffic: One-Third Is Bogus

    Billions of dollars are flowing into online advertising. But marketers also are confronting an uncom ...

  6. 【WebMisCentral WMC】基于Extjs 4.2x的企业级用户授权认证中心系统(SSO+AM+SM),多租户SAAS应用

    http://saas.chinacloudtech.com 题记 三年磨一剑,在企业信息化的道路上已经走了3年之久了,3年多时间里做了很多,突破了很多:有无奈和辛酸,也有收货与喜悦:自我价值也在不断 ...

  7. ubuntu-16.04+-xxx-i386.iso :安装 Oracle 11gR2 数据库

    前言:说实在的,ubuntu 16.04以上很难安装oracle!其间走过了艰难的一段路! 重要附件:ubuntu16.04+-xxx-i386.iso_安装oracle所需的软件包.zip 特点: ...

  8. 关于castle和Could not find the dialect in the configuration错误

    最近试了试castle,NND,老是报“Could not find the dialect in the configuration”,明明配的没问题,上网搜索所有教程都是这么配的.后来在一国外的网 ...

  9. 全国DNS服务器IP地址【电信、网通、铁通】

    免费DNS地址: 114DNS:114.114.114.114(推荐国内使用) Google DNS:8.8.8.8(国外) ************************************* ...

  10. 深入理解图优化与g2o:图优化篇

    前言 本节我们将深入介绍视觉slam中的主流优化方法——图优化(graph-based optimization).下一节中,介绍一下非常流行的图优化库:g2o. 关于g2o,我13年写过一个文档,然 ...