Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for rendering IEnumerable entities #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public IActionResult Index(string id)
query = query.Include(property.Name);
}

viewModel.Data = (IEnumerable<object>)query;
viewModel.Data = query.ToArray();
viewModel.DbContext = dbContextObject;
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/DotNetEd.CoreAdmin/Views/CoreAdminData/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using DotNetEd.CoreAdmin
@using System.Collections
@using DotNetEd.CoreAdmin
@using Microsoft.Extensions.Localization
@using NonFactors.Mvc.Grid
@using System.Linq.Expressions
Expand Down Expand Up @@ -61,7 +62,27 @@
var base64 = ImageUtils.WebBase64EncodeImageByteArrayOrNull(lambda.Compile().Invoke(value));
return base64 == null ? string.Empty : $"<img height='48' src='{base64}'>";
} ).Encoded(false);
}
}
else if (entityProperty.PropertyType.IsAssignableTo(typeof(IEnumerable)) && entityProperty.PropertyType != typeof(string))
{
var lambda = Expression.Lambda<Func<object, object>>(property, entity);

columns.Add(lambda).Titled(entityProperty.Name)
.RenderedAs((value) =>
{
var finalString = "<ul>";
try
{
foreach (var raw in (IEnumerable)entityProperty.GetValue(value)!)
{
finalString += $"<li><a href=\"{Url.Action("Index", new { id = raw.GetType().Name })}\">{raw}</a></li>";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings behave like value variables and each time you add to them create another variable and its not optimum, better to use stringbuilder

}
}
catch(NullReferenceException) {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with that? Consuming exceptions like that is an anti-pattern. Shouldn't there be a way to avoid an NRE in the first place?

finalString += "</ul>";
return finalString;
} ).Encoded(false);
}
/* if (entityProperty.PropertyType == typeof(Guid))
{
var lambda = Expression.Lambda<Func<object,Guid>>(property, entity);
Expand Down