Advertisement
Guest User

Untitled

a guest
Mar 28th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. @model EntityBase
  2. @using System.ComponentModel
  3. @using System.ComponentModel.DataAnnotations
  4. @using System.Reflection
  5. @using Microsoft.AspNetCore.Mvc.Localization
  6. @using Microsoft.Extensions.Localization
  7.  
  8. @inject IHtmlLocalizer<DataAnnotationResources> DataAnnotationLocalizer
  9. @inject IStringLocalizer<SharedResources> SharedLocalizer
  10.  
  11. @{
  12. bool IsReadOnly(CustomAttributeData a) => a.AttributeType == typeof(ReadOnlyAttribute) && (bool)a.ConstructorArguments[0].Value;
  13. bool IsHidden(CustomAttributeData a) => a.AttributeType == typeof(KeyAttribute) || a.AttributeType == typeof(HiddenInputAttribute);
  14.  
  15. // Virtual Properties are relationship proxies.
  16. var nonVirtualProps = Model.GetType().GetProperties().Where(p => !p.GetGetMethod().IsVirtual).ToArray();
  17.  
  18. // Sort the fields into buckets.
  19. var regularInputProperties = nonVirtualProps.Where(p =>
  20. p.SetMethod != null
  21. && !p.CustomAttributes.Any(IsHidden)
  22. && !p.CustomAttributes.Any(IsReadOnly)
  23. ).OrderBy(p => p.CustomAttributes.FirstOrDefault(a =>
  24. a.AttributeType == typeof(DisplayAttribute))?.NamedArguments.FirstOrDefault(arg =>
  25. arg.MemberName == "Order").TypedValue.Value).ToArray();
  26.  
  27. var hiddenInputProperties = nonVirtualProps.Where(p => p.CustomAttributes.Any(IsHidden))
  28. .OrderBy(p =>
  29. p.CustomAttributes.FirstOrDefault(a =>
  30. a.AttributeType == typeof(DisplayAttribute))?.NamedArguments.FirstOrDefault(arg =>
  31. arg.MemberName == "Order").TypedValue.Value).ToArray();
  32.  
  33. var readOnlyProperties = nonVirtualProps.Where(p => p.CustomAttributes.Any(IsReadOnly))
  34. .OrderBy(p =>
  35. p.CustomAttributes.FirstOrDefault(a =>
  36. a.AttributeType == typeof(DisplayAttribute))?.NamedArguments.FirstOrDefault(arg =>
  37. arg.MemberName == "Order").TypedValue.Value).ToArray();
  38.  
  39.  
  40. // Used in row building below.
  41. var rowFlag = false;
  42. const string colCap = @"
  43. </div>
  44. <div class=""col-md-6 col-12"">";
  45. const string rowCap = @"
  46. </div>
  47. </div>
  48. <div class=""form-row"">
  49. <div class=""col-md-6 col-12"">";
  50. }
  51.  
  52. <form method="post">
  53. <div class="form-row">
  54. <div class="col-12">
  55. <div asp-validation-summary="All" class="text-danger"></div>
  56. @foreach (var prop in hiddenInputProperties)
  57. {
  58. <input type="hidden" id="@prop.Name" name="@prop.Name" value="@prop.GetValue(Model)">
  59. }
  60. @if (Model.GetId() != Guid.Empty)
  61. {
  62. <input type="hidden" id="RecordId" name="RecordId" value="@Model.GetId()">
  63. }
  64. <input type="hidden" id="CreatedBy" name="CreatedBy" value="@Model.CreatedBy">
  65. <input type="hidden" id="CreatedDate" name="CreatedDate" value="@Model.CreatedDate">
  66. <input type="hidden" id="ModifiedBy" name="ModifiedBy" value="@Model.ModifiedBy">
  67. <input type="hidden" id="ModifiedDate" name="ModifiedDate" value="@Model.ModifiedDate">
  68. <input type="hidden" id="IsActive" name="IsActive" value="@(Model.IsActive ? "true" : "false")">
  69. </div>
  70. </div>
  71. <div class="form-row">
  72. <div class="col-md-6 col-12">
  73. @foreach (var prop in regularInputProperties)
  74. {
  75. //AttributeCollection attributes = TypeDescriptor.GetProperties(Model)[prop.Name].Attributes;
  76. //var isReadOnly = attributes[typeof(ReadOnlyAttribute)].Equals(ReadOnlyAttribute.Yes);
  77.  
  78. var uiHintAttribute = prop.CustomAttributes
  79. .FirstOrDefault(a =>a.AttributeType == typeof(UIHintAttribute));
  80.  
  81. var displayName = (string)prop.CustomAttributes
  82. .FirstOrDefault(a => a.AttributeType == typeof(DisplayNameAttribute))?.ConstructorArguments[0].Value
  83. ?? (string)prop.CustomAttributes
  84. .FirstOrDefault(a => a.AttributeType == typeof(DisplayAttribute))?.NamedArguments
  85. .FirstOrDefault(arg => arg.MemberName == "Name").TypedValue.Value;
  86.  
  87. if (string.IsNullOrEmpty(displayName))
  88. {
  89. continue;
  90. }
  91. var displayFormatString = (string)prop.CustomAttributes.FirstOrDefault(a =>
  92. a.AttributeType == typeof(DisplayFormatAttribute))?.NamedArguments.FirstOrDefault(arg =>
  93. arg.MemberName == "DataFormatString").TypedValue.Value;
  94.  
  95. var localizedDisplayName = DataAnnotationLocalizer[displayName];
  96.  
  97. var propType = prop.PropertyType;
  98. if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
  99. {
  100. // Get the real type of this nullable property.
  101. propType = propType.GetGenericArguments().Single();
  102. }
  103. var inputType = "";
  104. if (propType == typeof(Guid))
  105. {
  106. <div class="form-group">
  107. <label for="@prop.Name">
  108. @localizedDisplayName
  109. </label>
  110. <select class="form-control" name="@prop.Name" id="@prop.Name">
  111. <option value="">@SharedLocalizer["SelectDefaultItem"]</option>
  112. @{
  113. var options = (IEnumerable<SelectListItem>)ViewData[$"{prop.Name}Options"];
  114. if (options != null && options.Any())
  115. {
  116. foreach (var option in options)
  117. {
  118. if (prop.GetValue(Model)?.ToString().Equals(option.Value, StringComparison.InvariantCultureIgnoreCase) ?? false)
  119. {
  120. <option value="@option.Value" selected="selected">@option.Text</option>
  121. }
  122. else
  123. {
  124. <option value="@option.Value">@option.Text</option>
  125. }
  126. }
  127. }
  128. }
  129. </select>
  130. </div>
  131. }
  132. else if (uiHintAttribute != null && string.Equals(uiHintAttribute.ConstructorArguments[0].Value.ToString(), "DropDownList", StringComparison.InvariantCultureIgnoreCase))
  133. {
  134. <div class="form-group">
  135. <label for="@prop.Name">
  136. @localizedDisplayName
  137. </label>
  138. <select class="form-control" name="@prop.Name" id="@prop.Name">
  139. <option value="">@SharedLocalizer["SelectDefaultItem"]</option>
  140. @{
  141. var options = (IEnumerable<SelectListItem>)ViewData[$"{prop.Name}Options"];
  142. if (options != null && options.Any())
  143. {
  144. foreach (var option in options)
  145. {
  146. if (prop.GetValue(Model)?.ToString().Equals(option.Value, StringComparison.InvariantCultureIgnoreCase) ?? false)
  147. {
  148. <option value="@option.Value" selected="selected">@option.Text</option>
  149. }
  150. else
  151. {
  152. <option value="@option.Value">@option.Text</option>
  153. }
  154. }
  155. }
  156. }
  157. </select>
  158. </div>
  159. }
  160. else if (propType == typeof(string))
  161. {
  162. inputType = "text";
  163. }
  164. else if (propType == typeof(int) || propType == typeof(decimal))
  165. {
  166. inputType = "number";
  167. }
  168. else if (propType == typeof(DateTimeOffset))
  169. {
  170. inputType = "datetime";
  171. }
  172. else if (propType == typeof(bool))
  173. {
  174. inputType = "checkbox";
  175. }
  176. else
  177. {
  178. throw new NotImplementedException($"The Editor Template lacks a display definition for type {propType}.");
  179. }
  180.  
  181. if (inputType == "checkbox")
  182. {
  183. <div class="btn-group-toggle" data-toggle="buttons">
  184. <label class="btn btn-secondary">
  185. @Html.CheckBox(prop.Name, (bool)prop.GetValue(Model)) @localizedDisplayName
  186. </label>
  187. </div>
  188. }
  189. else
  190. {
  191. <div class="form-group">
  192. <label for="@prop.Name">
  193. @localizedDisplayName
  194. </label>
  195. <input class="form-control" type="@inputType" id="@prop.Name" name="@prop.Name"
  196. value="@(string.IsNullOrEmpty(displayFormatString)
  197. ? prop.GetValue(Model)
  198. : string.Format(displayFormatString, prop.GetValue(Model)))">
  199. </div>
  200. }
  201. if (!rowFlag)
  202. {
  203. @Html.Raw(colCap)
  204. rowFlag = true;
  205. }
  206. else
  207. {
  208. @Html.Raw(rowCap)
  209. rowFlag = false;
  210. }
  211. }
  212.  
  213. </div>
  214. </div>
  215. <div class="form-row">
  216. <div class="col-12">
  217. <div class="form-group">
  218. <input type="submit" value="Save" class="btn btn-primary" />
  219. </div>
  220. </div>
  221. </div>
  222. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement