Skip to content

Instantly share code, notes, and snippets.

@krcourville
Created October 3, 2013 12:27
Show Gist options
  • Select an option

  • Save krcourville/6809041 to your computer and use it in GitHub Desktop.

Select an option

Save krcourville/6809041 to your computer and use it in GitHub Desktop.
Dumping object to HTML table with a Razor view: Here's a razor view for rendering an object to html table. Includes support for arrays, anonymous object, and complex objects with twitter bootstrap formatting. Note: CamelToTitleCase() is a string extension function.
@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>@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