Created
May 15, 2018 17:39
-
-
Save WrackedFella/b2e84e2accc0bdc0f3fa1b409561badc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model object | |
@{var enumerableModelData = ViewData.ModelMetadata.Model as IEnumerable<object>;} | |
@if (enumerableModelData != null) | |
{ | |
if(enumerableModelData.Count() < 1) | |
{ | |
@SiteStrings.NoData | |
return; | |
} | |
// Gotta use some reflection to build tables. | |
// ToDo: Find a better way to do this? | |
var modelType = enumerableModelData.FirstOrDefault().GetType(); | |
var properties = modelType.GetProperties() | |
.Where(p => p.CustomAttributes.All(ca => ca.AttributeType.Name != "HiddenInputAttribute") | |
&& (p.GetCustomAttributes(typeof(BuildAForm.Web.Attributes.VisibilityAttribute), false).Cast<BuildAForm.Web.Attributes.VisibilityAttribute>().SingleOrDefault()?.ShowForDisplay ?? true) == true) | |
.Select(p => new | |
{ | |
PropertyName = p.Name, | |
DislayName = p.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false) | |
.Cast<System.ComponentModel.DataAnnotations.DisplayAttribute>().SingleOrDefault()?.GetName(), | |
DataFormatString = p.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayFormatAttribute), false) | |
.Cast<System.ComponentModel.DataAnnotations.DisplayFormatAttribute>().SingleOrDefault()?.DataFormatString | |
}); | |
<table class="table table-sm table-hover"> | |
@if(properties.Any(x => !string.IsNullOrEmpty(x.DislayName))) | |
{ | |
<thead> | |
<tr> | |
@foreach (var prop in properties) | |
{ | |
@Html.Raw($"<th>{prop.DislayName}</th>") | |
} | |
</tr> | |
</thead> | |
} | |
<tbody> | |
@foreach (var record in enumerableModelData) | |
{ | |
var values = record.GetType().GetProperties() | |
.Where(x => properties.Any(y => y.PropertyName == x.Name)) | |
.Select(x => string.Format(properties.Single(y => y.PropertyName == x.Name)?.DataFormatString ?? "{0}", x.GetValue(record))); | |
<tr scope="row"> | |
@foreach (var val in values) | |
{ | |
@Html.Raw($"<td>{val}</td>"); | |
} | |
</tr> | |
} | |
</tbody> | |
</table> | |
} | |
else | |
{ | |
// Construct the view from metadata. | |
<div class="form-row"> | |
@{ bool flag = false; } | |
@foreach (var property in ViewData.ModelMetadata.Properties.Where(x => x.ShowForDisplay)) | |
{ | |
if (property.TemplateHint == "HiddenInput") | |
{ | |
@Html.Hidden(property.PropertyName) | |
continue; | |
} | |
<div class="form-group col-lg-6"> | |
<label class="col-form-label" for="@property.PropertyName"> | |
@(property.DisplayName ?? property.PropertyName) | |
</label> | |
<span class="form-text"> | |
@if (property.IsComplexType) | |
{ | |
foreach (var subProp in property.Properties) | |
{ | |
if (subProp.PropertyName == "DisplayName" || subProp.PropertyName == "Name" || subProp.PropertyName == "Value") | |
{ | |
@(String.Format(subProp.DisplayFormatString ?? "{0}", subProp.Model)); | |
break; | |
} | |
} | |
} | |
else | |
{ | |
@(String.Format(property.DisplayFormatString ?? "{0}", property.Model)) | |
} | |
</span> | |
</div> | |
if (flag) | |
{ | |
@Html.Raw("</div><div class='form-row'>"); | |
} | |
flag = !flag; | |
} | |
</div> | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment