Guest User

Untitled

a guest
Dec 14th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. @inherits ViewPage<GetCustomersubscriptionsResponse>
  2.  
  3. @{
  4. ViewBag.Title = string.Format("History > subscriptions > Customer {0}", Model.CustomerId);
  5. Layout = "CustomerOfficeUIFabric";
  6. }
  7. <div class="tableContainer">
  8. @if (Model.subscriptions != null && Model.subscriptions.Count > 0)
  9. {
  10. <table class="ms-Table" style="max-width:800px;">
  11. <thead>
  12. <tr>
  13. @{
  14. Type subscriptionType = Model.subscriptions.GetType().GetGenericArguments()[0];
  15. }
  16. @Html.GenerateHeadings(subscriptionType)
  17. </tr>
  18. </thead>
  19. <tbody>
  20. @foreach (var subscription in Model.subscriptions)
  21. {
  22. @Html.GenerateRow(subscription)
  23. }
  24. </tbody>
  25. </table>
  26. }
  27. else
  28. {
  29. <div class="notFound ms-font-m-plus">No records found</div>
  30. }
  31. </div>
  32.  
  33. public static class HtmlHelperExtensions
  34. {
  35. public static MvcHtmlString GenerateRow(this HtmlHelper htmlHelper, object Subscription)
  36. {
  37. var sb = new StringBuilder();
  38. sb.Append("<tr>");
  39. Type SubscriptionType = Subscription.GetType();
  40. foreach (PropertyInfo propertyInfo in SubscriptionType.GetProperties())
  41. {
  42. object propertyValue = propertyInfo.GetValue(Subscription, null);
  43. sb.Append($"<td>{propertyValue}</td>");
  44. }
  45. sb.Append("</tr>");
  46.  
  47. return new MvcHtmlString(sb.ToString());
  48. }
  49.  
  50. public static MvcHtmlString GenerateHeadings(this HtmlHelper htmlHelper, Type modelType)
  51. {
  52. var sb = new StringBuilder();
  53.  
  54. List<string> displayNames = GetDisplayNames(modelType);
  55.  
  56. foreach (var displayName in displayNames)
  57. {
  58. sb.Append($"<th>{displayName}</th>");
  59. }
  60.  
  61. return new MvcHtmlString(sb.ToString());
  62. }
  63.  
  64. private static List<string> GetDisplayNames(Type modelType)
  65. {
  66. List<string> displayNames = new List<string>();
  67.  
  68. PropertyInfo[] props = modelType.GetProperties();
  69. foreach (PropertyInfo prop in props)
  70. {
  71. string displayNameAttributeValue = GetDisplayNameAttributeValue(prop);
  72. string heading = !string.IsNullOrWhiteSpace(displayNameAttributeValue) ? displayNameAttributeValue : prop.Name;
  73. displayNames.Add(heading);
  74. }
  75.  
  76. return displayNames;
  77. }
  78.  
  79. private static string GetDisplayNameAttributeValue(PropertyInfo prop)
  80. {
  81. object[] attributes = prop.GetCustomAttributes(false);
  82. if (attributes.Any())
  83. {
  84. var displayNameAttributes = attributes.Where(x => x is DisplayNameAttribute);
  85. if (displayNameAttributes.Any())
  86. {
  87. var displayNameAttribute = displayNameAttributes.First() as DisplayNameAttribute;
  88. return displayNameAttribute.DisplayName;
  89. }
  90. }
  91. return null;
  92. }
  93. }
Add Comment
Please, Sign In to add comment