using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Web;
|
|
namespace WebApi_QQJF.WebHelper
|
{
|
public static class WebHelper
|
{
|
/// <summary>
|
/// 获取request属性值
|
/// </summary>
|
/// <param name="obj"></param>
|
/// <param name="fieldName"></param>
|
public static void GetRequest(HttpRequestBase request, object model)
|
{
|
int i;
|
decimal d;
|
DateTime time;
|
|
try
|
{
|
Type type = model.GetType();
|
foreach (var p in type.GetProperties())
|
{
|
string propertyValue = request.Form[p.Name];
|
|
//通用大写model
|
if (string.IsNullOrEmpty(propertyValue))
|
{
|
propertyValue = request.Form[p.Name.ToUpper()];
|
}
|
|
if (p.PropertyType == typeof(decimal?))
|
{
|
if (!string.IsNullOrEmpty(propertyValue) && decimal.TryParse(propertyValue, out d))
|
{
|
p.SetValue(model, d);
|
}
|
}
|
else if (p.PropertyType == typeof(int?))
|
{
|
if (!string.IsNullOrEmpty(propertyValue) && int.TryParse(propertyValue, out i))
|
{
|
p.SetValue(model, i);
|
}
|
}
|
else if (p.PropertyType == typeof(DateTime?))
|
{
|
if (!string.IsNullOrEmpty(propertyValue) && DateTime.TryParse(propertyValue, out time))
|
{
|
p.SetValue(model, time);
|
}
|
}
|
else if (p.PropertyType == typeof(string))
|
{
|
if (!string.IsNullOrEmpty(propertyValue))
|
{
|
p.SetValue(model, propertyValue);
|
}
|
}
|
else if (p.PropertyType == typeof(bool) || p.PropertyType == typeof(bool?))
|
{//支持bool类型 【EditBy shaocx,2022-11-1】
|
if (!string.IsNullOrEmpty(propertyValue))
|
{
|
p.SetValue(model, Convert.ToBoolean(propertyValue));
|
}
|
}
|
}
|
|
|
}
|
catch (Exception e)
|
{
|
return;
|
}
|
|
}
|
}
|
}
|