Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. public class ViewModelBinder<T> : IModelBinder where T: class
  2. {
  3. #region Private Fields
  4.  
  5. private ModelBindingContext _bindingContext;
  6. private ControllerContext _controllerContext;
  7.  
  8. #endregion
  9.  
  10. /// <summary>
  11. /// ADDS MODEL STATE ERRORS
  12. /// </summary>
  13. /// <param name="name">Name of Error</param>
  14. /// <param name="message">Error Message</param>
  15. private void AddModelError(string name, string message)
  16. {
  17. _bindingContext.ModelState.AddModelError(name, message);
  18. }
  19.  
  20. /// <summary>
  21. /// Recursively Create View Model Class, Instantiating classes along the way and populating their values
  22. /// </summary>
  23. /// <param name="_returnValue">Class to create - first = Type of T</param>
  24. private void CreateModel(ref object _returnValue)
  25. {
  26. foreach (var item in _returnValue.GetType().GetProperties().ToList())
  27. {
  28. var itemType = item.PropertyType;
  29. if (!itemType.IsPrimitive && !itemType.FullName.StartsWith("System."))
  30. {
  31. var innerClass = Activator.CreateInstance(itemType);
  32. CreateModel(ref innerClass);
  33. item.SetValue(_returnValue, innerClass, null);
  34. }
  35. else
  36. {
  37. if (HasValue(_controllerContext.HttpContext.Request.Form, item.Name))
  38. // if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {…}
  39. item.SetValue(_returnValue, Convert.ChangeType(_controllerContext.HttpContext.Request.Form[item.Name], item.PropertyType), null);
  40. }
  41. }
  42.  
  43. // VALIDATE MAIN VIEW MODEL
  44. ValidateModel(_returnValue);
  45. }
  46.  
  47. /// <summary>
  48. /// GENERATE VIEW MODEL
  49. /// </summary>
  50. /// <param name="controllerContext">Contains controller information</param>
  51. /// <param name="bindingContext">Contains binding information</param>
  52. /// <returns>vIEW MODEL OF TYPE T</returns>
  53. public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  54. {
  55. _bindingContext = bindingContext;
  56. _controllerContext = controllerContext;
  57. var returnValue = Activator.CreateInstance(typeof(T));
  58.  
  59. CreateModel(ref returnValue);
  60.  
  61. return returnValue;
  62. }
  63.  
  64. /// <summary>
  65. /// Check to see if key is in form collection
  66. /// </summary>
  67. /// <param name="form">Request Form</param>
  68. /// <param name="key">Key to key to check for (name of property)</param>
  69. /// <returns>bool - if true then key is in collection</returns>
  70. private bool HasValue(NameValueCollection form, string key)
  71. {
  72. return form.AllKeys.ToList().Contains(key);
  73. }
  74.  
  75. /// <summary>
  76. /// create a validation context for a given model and check for validation errors
  77. /// </summary>
  78. /// <param name="model">model to validate</param>
  79. private void ValidateModel(object model)
  80. {
  81. var vc = new ValidationContext(model);
  82. var results = new List<ValidationResult>();
  83. var isValid = Validator.TryValidateObject(model, vc, results, true);
  84.  
  85. if (isValid) return;
  86.  
  87. foreach (var error in results)
  88. AddModelError(error.ErrorMessage, error.ErrorMessage);
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement