ke_junjie
2025-06-04 101c57ec4c28bc3c36e49c50a926e9e7c0dd0247
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
            }
 
        }
    }
}