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);
}
}
}
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);
}
}
}