HTTP上下文表单内容转为实体对象
using ServiceStack.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace restService.Interface.Helper
{
public static class EntityHelper
{
/// <summary>
/// 将HTTP上下文表单内容转为实体对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static T RequestToModel<T>(this HttpContext context) where T : new()
{
T model = new T();
Type type = model.GetType();
PropertyInfo[] Fields = type.GetProperties();
for (int i = ; i < Fields.Count(); i++)
{
var name = Fields[i].Name;
Type vtype = Fields[i].PropertyType;
var value = context.Request[name];
if (value != null)
{
if (vtype.Name.ToLower().IndexOf("int") >= )//int
{
var v = Convert.ToInt32(value);
Fields[i].SetValue(model, v);
continue;
}
if (vtype.Name.ToLower().IndexOf("string") >= )//string
{
var v = Convert.ToString(value);
Fields[i].SetValue(model, v);
continue;
}
if (vtype.Name.ToLower().IndexOf("datetime") >= )//datetime
{
var v = Convert.ToDateTime(value);
Fields[i].SetValue(model, v);
}
if (vtype.Name.ToLower().IndexOf("bool") >= )//datetime
{
var v = Convert.ToBoolean(value);
Fields[i].SetValue(model, v);
}
}
}
return model;
}
/// <summary>
/// 将HTTP上下文表单内容转为实体对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static T RequestToModel<T>(this IRequest context) where T : new()
{
T model = new T();
Type type = model.GetType();
PropertyInfo[] Fields = type.GetProperties();
for (int i = ; i < Fields.Count(); i++)
{
var name = Fields[i].Name;
Type vtype = Fields[i].PropertyType;
string value = context.QueryString[name];
if(value==null)
value = context.FormData[name];
if (value != null)
{
if (vtype.Name.ToLower().IndexOf("int") >= )//int Nullable
{
var v = Convert.ToInt32(value);
Fields[i].SetValue(model, v);
continue;
}
if (vtype.Name.ToLower().IndexOf("nullable") >= )//Nullable
{
var v = Convert.ToDecimal(value);
Fields[i].SetValue(model, v);
continue;
}
if (vtype.Name.ToLower().IndexOf("string") >= )//string
{
var v = Convert.ToString(value);
Fields[i].SetValue(model, v);
continue;
}
if (vtype.Name.ToLower().IndexOf("datetime") >= )//datetime
{
var v = Convert.ToDateTime(value);
Fields[i].SetValue(model, v);
}
if (vtype.Name.ToLower().IndexOf("bool") >= )//datetime
{
var v = Convert.ToBoolean(value);
Fields[i].SetValue(model, v);
}
}
}
return model;
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="context"></param>
/// <param name="key">关键字</param>
/// <returns></returns>
public static String GetDataParameter(this IRequest context,string key)
{
string result = context.FormData[key];
if (result == null)
result = context.QueryString[key];
return result;
}
}
}
HTTP上下文表单内容转为实体对象的更多相关文章
- 将form表单元素转为实体对象 或集合 -ASP.NET C#
简介: 做WEBFROM开发的同学都知道后台接收参数非常麻烦 虽然MVC中可以将表单直接转为集实,但不支持表单转为 LIST<T>这种集合 单个对象的用法: 表单: <input n ...
- spring mvc表单自动装入实体对象
<form action="/springmvc1/user/add" method="post"> id: <input type=&quo ...
- 序列化form表单内容为json对象
SourceCode: ; (function ($) { $.fn.extend({ serializeJson: function () { var json = {}; $(this.seria ...
- 拓展jQuery的serialize(),将form表单转化为json对象
jQuery 的 serialize() 方法经常会报 Uncaught TypeError: JSON.serializeObject is not a function 的错误, 原装的方法真的一 ...
- form表单提交转为可被 getModel(PROJECT.class ,null);接收
var form = new mini.Form("#editForm"+id); form.validate();if (!form.isValid()) { alert('信息 ...
- 023-将表单序列化为json对象
使用jQuery将表单序列化为json对象,其中serializeJson方法的名字任意,serializeArray()这个jQuery提供的方法.this指的就是谁调用了这个方法. $.fn.se ...
- 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...
- jQuery表单验证以及将表单序列化为json对象小练习
jquery表单验证(非实时验证),同时,将表单序列化为json对象提交表单. <!DOCTYPE html> <html lang="en"> <h ...
- c#程序为PDF文件填写表单内容
众所周知,PDF文件一般情况下是无法修改的,如果你有一张现成的PDF表格,这时想通过编程实现从数据库或者动态生成内容去填写这张表格,就会有些问题了,首先我们要解决以下2个重要的问题: 1.如何将内容写 ...
随机推荐
- codeforce 459 DIV2 D题
题意 在一个DAG上面有N个点M条边,每一条边上都有一个小写字母.两个人Max and Lucas 每个人一颗棋子,两个人轮流行棋,当前这一步选择的路上面的字母必须大于等于上一步路上面的字母,当轮 ...
- 用Eclipse Memory Analyzer查找内存泄露
写在CSDN里面了 http://blog.csdn.net/dayulxl/article/details/78164301
- 一个小仓鼠的js动画
直接在网页打开就可以玩了: http://cdn.abowman.com/widgets/hamster/hamster.swf?up_bodyColor=f0e9cc&up_feetColo ...
- fopencookie函数详解
今天看DPDK时,看到了fopencookie函数,以前基本没有用过该函数,乘此机会好好看看如何使用. 1. 函数头文件与函数原型 函数头文件: #include <stdio.h> 函数 ...
- Ubuntu14.04下安装glog
下载原始代码编译 1. Clone Source Code glog git clone https://github.com/google/glog 2. Install dependencies ...
- 39.FORMAT() 函数
FORMAT() 函数 FORMAT 函数用于对字段的显示进行格式化. SQL FORMAT() 语法 SELECT FORMAT(column_name,format) FROM table_nam ...
- setnx()
setnx(key,value):当指定的key不存在时,为你设置指定的值
- js实现在表格中删除和添加一行
<!DOCTYPE html><html> <head> <title> new document </title> <meta ht ...
- redis过期key的清理策略
一,有三种不同的删除策略(1),立即清理.在设置键的过期时间时,创建一个回调事件,当过期时间达到时,由时间处理器自动执行键的删除操作. (2),惰性清理.键过期了就过期了,不管.当读/写一个已经过期的 ...
- Java 错误结果Throw/Throws
目录 java处理异常方式 throw的作用 throws的作用 方法原理 举例 总结 个人实例 1.java处理异常方式 在java代码中如果发生异常的话,jvm会抛出 ...