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.如何将内容写 ...
随机推荐
- Vulkan Tutorial 04 理解Validation layers
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 What are validation layers? Vulkan API的设计核 ...
- cuteFTP软件往linux中上传文件时报…
我是在win7和VM中的ubuntu传输文件: 使用一个客户端,可以正常的连接,但是当上传文件时,总是报553 Could not create file错误信息. 主要原因是新建的文件夹没有更改权限 ...
- const int *pi与int&nbs…
此质料是摘要:<<彻底搞定C 指针 >>,自己感觉比较有价值,现与大家分享. 1. 从const int i 说起 你知道我们声明一个变量时象这样int i :这个i是可能在它 ...
- Laravel 5.3 auth中间件底层实现详解(转)
1. 注册认证中间件, 在文件 app/Http/Kernel.php 内完成: protected $routeMiddleware = [ 'auth' => \Illuminate\Aut ...
- docker swarm && compose 示例
docker swarm 创建docker swarm集群 //master节点操作 docker swarm init --advertise-addr materip //node节点操作 -1x ...
- 【总结整理】webGIS学习thinkGIS(四)WebGIS中通过行列号来换算出多种瓦片的URL 之离线地
http://www.thinkgis.cn/topic/541a5319da8db186fd06e097 1.前言 在前面我花了两个篇幅来讲解行列号的获取,也解释了为什么要获取行列号.在这一章,我将 ...
- Docker02 基本命令、开发环境搭建、docker安装nginx、Dockerfile、路径挂载
1 基本命令 1.1 docker相关 centos6.5 安装docker环境 >sudo yum install -y http://mirrors.yun-idc.com/epel/6/i ...
- Navicat 连接阿里云的 MySQL
1 利用Navicat连接阿里云服务器中MySQL 常规连接方式常用于连接本机数据库以及虚拟机中的数据库:但是连接阿里云的MySQL数据库时会出现连接不上的情况,错误信息如下: 1.1 常规中的配置 ...
- lucene和solr
我们为什么要用solr呢? 1.solr已经将整个索引操作功能封装好了的搜索引擎系统(企业级搜索引擎产品) 2.solr可以部署到单独的服务器上(WEB服务),它可以提供服务,我们的业务系统就只要发送 ...
- xgboost 并行调参
Parallelism When Cross Validating XGBoost Models This raises the question as to how cross validation ...