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上下文表单内容转为实体对象的更多相关文章

  1. 将form表单元素转为实体对象 或集合 -ASP.NET C#

    简介: 做WEBFROM开发的同学都知道后台接收参数非常麻烦 虽然MVC中可以将表单直接转为集实,但不支持表单转为 LIST<T>这种集合 单个对象的用法: 表单: <input n ...

  2. spring mvc表单自动装入实体对象

    <form action="/springmvc1/user/add" method="post"> id: <input type=&quo ...

  3. 序列化form表单内容为json对象

    SourceCode: ; (function ($) { $.fn.extend({ serializeJson: function () { var json = {}; $(this.seria ...

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

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

  5. form表单提交转为可被 getModel(PROJECT.class ,null);接收

    var form = new mini.Form("#editForm"+id); form.validate();if (!form.isValid()) { alert('信息 ...

  6. 023-将表单序列化为json对象

    使用jQuery将表单序列化为json对象,其中serializeJson方法的名字任意,serializeArray()这个jQuery提供的方法.this指的就是谁调用了这个方法. $.fn.se ...

  7. 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...

  8. jQuery表单验证以及将表单序列化为json对象小练习

    jquery表单验证(非实时验证),同时,将表单序列化为json对象提交表单. <!DOCTYPE html> <html lang="en"> <h ...

  9. c#程序为PDF文件填写表单内容

    众所周知,PDF文件一般情况下是无法修改的,如果你有一张现成的PDF表格,这时想通过编程实现从数据库或者动态生成内容去填写这张表格,就会有些问题了,首先我们要解决以下2个重要的问题: 1.如何将内容写 ...

随机推荐

  1. Spring Cloud Eureka 3 (Eureka client注册服务提供者)

    在完成服务注册中心的搭建后我们来尝试下将一个既有的spring boot应用加入eureka的服务治理体系中 新建一个spring boot项目加入eureka client依赖 这里加入的eurek ...

  2. SQLSERVER索引在什么情况下会失效

    索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因) 注意:要想使用or,又想让索引生效,只能将or条件中的 ...

  3. SpringBoot16 MockMvc的使用、JsonPath的使用、请求参数问题、JsonView、分页查询参数、JsonProperty

    1 MockMvc的使用 利用MockMvc可以快速实现MVC测试 坑01:利用MockMvc进行测试时应用上下文路径是不包含在请求路径中的 1.1 创建一个SpringBoot项目 项目脚手架 1. ...

  4. PHP 5.5环境配置

    php5.5 + apache2.4 安装配置 1 2 3 4 5 6 7 分步阅读 php5.5 做了大量的更新,在与apache搭配的时候如何选择也很有讲究,这里我们以64位 php5.6 和 A ...

  5. 2-javascript::笔记

    0.位置: HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head& ...

  6. 如何在CentOS里切换操作系统所用的语言,中英文切换

    操作系统CentOS 7.5,安装的时候选择的事中文,后来想改成英文 1.点左上角的“应用程序”---->再点“系统工具”----->“设置” 2.点“区域语言”,再点右侧的“汉语(中国) ...

  7. 默默的发现在网上找到的hook NtQueryDirectoryFile......

    默默的发现在网上找到的hook  NtQueryDirectoryFile...... hook  NtQueryDirectoryFile是为了实现文件隐藏,然后就发现在网上发现的代码版本似乎同一个 ...

  8. laravel查询最后执行的一条sql语句

  9. cakephp重写配置

    开启重新: (1)开启服务器的mod_rewrite模块 (2)注释掉app/ConfigScore.php中的 Configure::write('App.baseUrl', env('SCRIPT ...

  10. spark源码阅读之network(2)

    在上节的解读中发现spark的源码中大量使用netty的buffer部分的api,该节将看到netty核心的一些api,比如channel: 在Netty里,Channel是通讯的载体(网络套接字或组 ...