Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class JsonBinderAttribute : CustomModelBinderAttribute
  2.     {
  3.         public override IModelBinder GetBinder()
  4.         {
  5.             return new JsonModelBinder();
  6.         }
  7.  
  8.         public class JsonModelBinder : IModelBinder
  9.         {
  10.             public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  11.             {
  12.                 try
  13.                 {
  14.                     if (controllerContext == null)
  15.                         throw new ArgumentNullException("controllerContext");
  16.                     if (bindingContext == null)
  17.                         throw new ArgumentNullException("bindingContext");
  18.  
  19.                     //object obj2 = Activator.CreateInstance(bindingContext.ModelType);
  20.                     Type genericType = new System.Web.Script.Serialization.JavaScriptSerializer().GetType();
  21.                     Type repositoryType = genericType;
  22.                     object repository = Activator.CreateInstance(repositoryType);
  23.  
  24.                     System.Reflection.MethodInfo genericMethod = repositoryType.GetMethod("Deserialize");
  25.                     System.Reflection.MethodInfo closedMethod = genericMethod.MakeGenericMethod(bindingContext.ModelType);
  26.                     return closedMethod.Invoke(repository, new[] { HttpContext.Current.Request[bindingContext.ModelName] });
  27.                 }
  28.                 catch
  29.                 {
  30.                     return null;
  31.                 }
  32.             }
  33.         }
  34.     }