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.如何将内容写 ...
随机推荐
- git用法小结(1)--建立远程仓库
最近一直在学习使用git来管理自己的程序,总是今天东学一点,明天西凑一点,到用的时候,总是有些茫然不知所措. 在博客园里看见一篇老好的文章,教我们做笔记啦,但是做完笔记还是要记得总结哦! 来吧,让我们 ...
- 17-EasyNetQ:非泛型的发布&订阅扩展方法
自从EasyNetQ第一个版本开始,它就可以发布/订阅特定类型的消息. bus.Subscribe<MyMessage>("subscriptionId", x =&g ...
- Xcode迁移工程常见问题
[Xcode迁移工程常见问题] 1.Header Search Paths (HEADER_SEARCH_PATHS) 是否设置正确.在Search Paths group下. 2.Framework ...
- PHP内核介绍及扩展开发指南—Extensions 的编写
Extensions 的编写 理解了这些运行机制以后,本章着手介绍Extensions 的编写,但凡写程序的人都知道hello world,那好,就从hello world开始. 1.1Hello W ...
- 238. Product of Array Except Self 由非己元素形成的数组
[抄题]: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- js 操作table
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs ...
- c语言学习笔记 break语句
比如 for() { for() { break; } } 那个break语句只是跳出它所在的那个for循环,不会把最外面的for循环都跳出去.
- RabbitMQ(pika模块)
RabbitMQ 基础 2 3 4 5 6 7 8 安装配置epel源 $ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-r ...
- LightOJ 1038 Race to 1 Again (概率DP,记忆化搜索)
题意:给定一个数 n,然后每次除以他的一个因数,如果除到1则结束,问期望是多少. 析:概率DP,可以用记忆公搜索来做,dp[i] = 1/m*sum(dp[j] + 1) + 1/m * (dp[i] ...
- orcad找不到dll
如果运行Capture.exe找不到cdn_sfl401as.dll,如果运行allegro.exe找不到cnlib.dll,(上面俩个库文件都在C:/Cadence/SPB_16.3/tools/b ...