MVC 传递复杂类型绑定模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.IO;
using Newtonsoft.Json;


namespace Lawyer.Web.Utility
{
    public class MvcJsonModelBinder:IModelBinder
    {


        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            try
            {
                //这是通过传递json字符串 直接解析 使用这种方法 比较直接
                  //var  createObj = System.Activator.CreateInstance(bindingContext.ModelType)
                  //     using(var  sr = controllerContext.HttpContext.Request.InputStream)
                  //     {
                  //      byte[] buff = new byte[1024];
                  //      sr.Read(buff, 0, buff.Length);
                  //      string json = System.Text.UTF8Encoding.UTF8.GetString(buff);
                  //      var jss = new JavaScriptSerializer();
                  //      return Newtonsoft.Json.JsonConvert.DeserializeObject(json, createObj.GetType());
                  // }
                //这种方法 通过 Form表单获取形式
                var createObj = System.Activator.CreateInstance(bindingContext.ModelType);
                foreach (var field in createObj.GetType().GetProperties())
                {
                    //获取绑定模型 字段属性 ,的request参数值
                    string json = controllerContext.RequestContext.HttpContext.Request[field.Name];
                    //如果是数组类型
                    if (json.StartsWith("[") && json.EndsWith("]"))
                    {
                        //是否是泛型
                        if (field.PropertyType.IsGenericType)
                        {
                            
                                Newtonsoft.Json.Linq.JArray jsonRsp = Newtonsoft.Json.Linq.JArray.Parse(json);
                                var td = field.PropertyType.GenericTypeArguments[0];
                                if (jsonRsp != null)
                                {
                                    object genericList = CreateGeneric(typeof(List<>), td);
                                    var addMethod = genericList.GetType().GetMethod("Add");
                                    for (int i = 0; i < jsonRsp.Count; i++)
                                    {
                                        var gobj = System.Activator.CreateInstance(td);
                                        JsonSerializer js = new JsonSerializer();
                                        object obj = js.Deserialize(jsonRsp[i].CreateReader(), td);
                                        addMethod.Invoke(genericList, new object[] { obj });
                                    }
                                    field.SetValue(createObj, genericList);
                                }
                        }


                    }
                    //如果是字符串类型
                    else
                    {
                        field.SetValue(createObj, controllerContext.RequestContext.HttpContext.Request[field.Name]);
                    }
                }
                return createObj;
            }
            catch (Exception ex)
            {
                throw new NotImplementedException();
            }


       
        }
        public static object CreateGeneric(Type generic, Type innerType, params object[] args)
        {
            Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
            return Activator.CreateInstance(specificType, args);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值