Last active
January 22, 2020 10:06
-
-
Save timku/86a0e55821ab073479618eb6652e021f to your computer and use it in GitHub Desktop.
ObjectTable.cshtml for dotnet core, dump your object into a table
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
<!-- | |
Tim Kuperus | |
Works for dotnet core | |
Name this ObjectTable.cshtml and place it in your views folder | |
--> | |
@model object | |
@{ | |
ViewBag.Title = "Object Information"; | |
} | |
@if (!Model.GetType().Name.Contains("Anonymous")) | |
{ | |
@foreach (var prop in Model.GetType().GetProperties()) | |
{ | |
<table class="table table-striped table-condensed table-bordered"> | |
<caption>Name: @Model.GetType().Name</caption> | |
<tbody> | |
<tr> | |
<td> | |
<strong>@prop.Name</strong> | |
</td> | |
<td> | |
@if (prop.PropertyType.IsArray) | |
{ | |
var arrayData = prop.GetValue(Model, null) as Array; | |
if (arrayData != null) | |
{ | |
foreach (var item in arrayData) | |
{ | |
@Html.Partial("ObjectTable", item) | |
} | |
} | |
} | |
else if (prop.PropertyType.Namespace == null || !prop.PropertyType.Namespace.StartsWith("System")) | |
{ | |
var data = prop.GetValue(Model, null); | |
if (data != null) | |
{ | |
@Html.Partial("ObjectTable", data) | |
} | |
} | |
else | |
{ | |
@Convert.ToString(prop.GetValue(Model, null)) | |
} | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment