Guest User

Untitled

a guest
Jul 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. namespace U413.UI.CustomModelBinders
  2. {
  3. /// <summary>
  4. /// Override for DefaultModelBinder in order to implement fixes to its behavior.
  5. /// </summary>
  6. public class U413ModelBinder : DefaultModelBinder
  7. {
  8. /// <summary>
  9. /// Fix for the default model binder's failure to decode enum types when binding to JSON.
  10. /// </summary>
  11. protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext,
  12. PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
  13. {
  14. var propertyType = propertyDescriptor.PropertyType;
  15. if (propertyType.IsEnum)
  16. {
  17. var providerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  18. if (null != providerValue)
  19. {
  20. var value = providerValue.RawValue;
  21. if (null != value)
  22. {
  23. var valueType = value.GetType();
  24. if (!valueType.IsEnum)
  25. {
  26. return Enum.ToObject(propertyType, value);
  27. }
  28. }
  29. }
  30. }
  31. return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
  32. }
  33. }
  34. }
  35.  
  36. protected override void OnApplicationStarted()
  37. {
  38. base.OnApplicationStarted();
  39.  
  40. AreaRegistration.RegisterAllAreas();
  41. RegisterRoutes(RouteTable.Routes);
  42.  
  43. // Register your new model binder
  44. ModelBinders.Binders.DefaultBinder = new U413ModelBinder();
  45. }
  46.  
  47. public class SomeModel
  48. {
  49. public MyEnum EnumValue { get; set; }
  50. public int BindToThisGuy
  51. {
  52. get { return (int) EnumValue; }
  53. set { EnumValue = (MyEnum)value; }
  54. }
  55. }
  56.  
  57. namespace MyApp.Enums
  58. {
  59. public enum ATS_Tabs { TabOne = 0, TabTwo = 1, TabThree = 2, TabFour = 3, TabFive = 4 };
  60.  
  61. public class ModelEnums
  62. {
  63. public static IEnumerable<Type> Types
  64. {
  65. get
  66. {
  67. List<Type> Types = new List<Type>();
  68. Types.Add(typeof(ATS_Tabs));
  69. return Types;
  70. }
  71. }
  72. }
  73. }
  74.  
  75. namespace MyApp.CustomModelBinders
  76. {
  77. public class EnumModelBinder : IModelBinder
  78. {
  79. public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  80. {
  81. ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  82. ModelState modelState = new ModelState { Value = valueResult };
  83. object actualValue = null;
  84.  
  85. try
  86. {
  87. return Enum.ToObject(Type.GetType(bindingContext.ModelType.AssemblyQualifiedName), Convert.ToInt32(valueResult.AttemptedValue));
  88. }
  89. catch (FormatException e)
  90. {
  91. modelState.Errors.Add(e);
  92. }
  93.  
  94. bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
  95. return actualValue;
  96. }
  97. }
  98. }
  99.  
  100. protected void Application_Start()
  101. {
  102. AreaRegistration.RegisterAllAreas();
  103.  
  104. foreach (Type type in ModelEnums.Types)
  105. {
  106. ModelBinders.Binders.Add(type, new EnumModelBinder());
  107. }
  108.  
  109. RegisterGlobalFilters(GlobalFilters.Filters);
  110. RegisterRoutes(RouteTable.Routes);
  111. }
Add Comment
Please, Sign In to add comment