diff --git a/src/DotNetEd.CoreAdmin/Controllers/CoreAdminDataController.cs b/src/DotNetEd.CoreAdmin/Controllers/CoreAdminDataController.cs index f2572f3..1c71c0d 100644 --- a/src/DotNetEd.CoreAdmin/Controllers/CoreAdminDataController.cs +++ b/src/DotNetEd.CoreAdmin/Controllers/CoreAdminDataController.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Reflection; @@ -37,6 +38,12 @@ public IActionResult Index(string id) viewModel.EntityType = dbSetProperty.PropertyType.GetGenericArguments().First(); viewModel.DbSetProperty = dbSetProperty; + if (Attribute.IsDefined(viewModel.EntityType, typeof(DisplayAttribute))) + { + viewModel.DbDisplayName = viewModel.EntityType.GetCustomAttribute().Name; + } + + var dbContextObject = (DbContext)this.HttpContext.RequestServices.GetRequiredService(dbSetEntity.DbContextType); var query = dbContextObject.Set(viewModel.EntityType); @@ -218,7 +225,8 @@ public IActionResult Create(string id) var autoGeneratedPropertyNames = newEntity.GetType().GetProperties() .Where(p => p.GetCustomAttributes().Any(a => a.GetType().Name.Contains("DatabaseGenerated"))).Select(p => p.Name); - + + ViewBag.DbDisplayName = entityType.GetCustomAttribute().Name ?? id; ViewBag.IgnoreFromForm = autoGeneratedPropertyNames; ViewBag.Relationships = relationships; return View(newEntity); @@ -234,6 +242,7 @@ public IActionResult EditEntity(string dbSetName, string id) .Where(p => p.GetCustomAttributes().Any(a => a.GetType().Name.Contains("DatabaseGenerated"))).Select(p => p.Name); ViewBag.DbSetName = dbSetName; + ViewBag.DbDisplayName = entityType.GetCustomAttribute().Name ?? dbSetName; ViewBag.Id = id; ViewBag.Relationships = relationships; ViewBag.IgnoreFromForm = databaseGeneratedProperties; @@ -242,6 +251,7 @@ public IActionResult EditEntity(string dbSetName, string id) + [HttpPost] public async Task EditEntityPost(string dbSetName, string id, [FromForm] object formData) { diff --git a/src/DotNetEd.CoreAdmin/DotNetEd.CoreAdmin.csproj b/src/DotNetEd.CoreAdmin/DotNetEd.CoreAdmin.csproj index 2f8049e..3d916d6 100644 --- a/src/DotNetEd.CoreAdmin/DotNetEd.CoreAdmin.csproj +++ b/src/DotNetEd.CoreAdmin/DotNetEd.CoreAdmin.csproj @@ -29,6 +29,7 @@ + @@ -37,6 +38,7 @@ + diff --git a/src/DotNetEd.CoreAdmin/JsonLocalizer.cs b/src/DotNetEd.CoreAdmin/JsonLocalizer.cs index 5c276b5..7964b11 100644 --- a/src/DotNetEd.CoreAdmin/JsonLocalizer.cs +++ b/src/DotNetEd.CoreAdmin/JsonLocalizer.cs @@ -36,7 +36,8 @@ public IEnumerable GetAllStrings(bool includeParentCultures) foreach(var culture in cultureKeys) { - var fileName = $"{culture}.json"; + //Firefox usually returns something like: "de,en-US;q=0.9,en;q=0.8" + var fileName = $"{culture.Split(',')[0]}.json"; var key = translations.Keys.FirstOrDefault(k => k.EndsWith(fileName)); if (key != null) { diff --git a/src/DotNetEd.CoreAdmin/Translations/de.json b/src/DotNetEd.CoreAdmin/Translations/de.json new file mode 100644 index 0000000..75428f3 --- /dev/null +++ b/src/DotNetEd.CoreAdmin/Translations/de.json @@ -0,0 +1,13 @@ +{ + "Create": "Erstellen", + "AccessDatabaseEntries": "Zugang der Datenbank auf der linken Seite", + "DevelopmentModeMessage": "Sie befinden sich im Entwicklermodus. Keine Authentisierung wurde für CoreAdmin festgelegt. Core Admin wird außerhalb vom Entwicklermodus nicht erreichbar sein. Siehe Dokumentation https://github.com/edandersen/core-admin.", + "CreateNew": "Neu Erstellen", + "GoBack": "Zurück", + "Edit": "Bearbeiten", + "Delete": "Löschen", + "DeleteConfirm": "Bestätige Löschung", + "Auto": "Auto", + "Dark": "Dunkel", + "Light": "Hell" +} diff --git a/src/DotNetEd.CoreAdmin/ViewComponents/CoreAdminMenuViewComponent.cs b/src/DotNetEd.CoreAdmin/ViewComponents/CoreAdminMenuViewComponent.cs index a38e947..e21bd1d 100644 --- a/src/DotNetEd.CoreAdmin/ViewComponents/CoreAdminMenuViewComponent.cs +++ b/src/DotNetEd.CoreAdmin/ViewComponents/CoreAdminMenuViewComponent.cs @@ -1,6 +1,9 @@ using DotNetEd.CoreAdmin.ViewModels; using Microsoft.AspNetCore.Mvc; +using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Threading.Tasks; namespace DotNetEd.CoreAdmin.ViewComponents @@ -22,6 +25,15 @@ public IViewComponentResult Invoke() { viewModel.DbContextNames.Add(dbSetEntity.DbContextType.Name); viewModel.DbSetNames.Add(dbSetEntity.Name); + + var d = dbSetEntity.Name; + if (Attribute.IsDefined(dbSetEntity.UnderlyingType, typeof(DisplayAttribute))) + { + var displayAttribute = dbSetEntity.UnderlyingType.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute; + if (!String.IsNullOrEmpty(displayAttribute.Name)) + d = displayAttribute.Name; + } + viewModel.DbDisplayNames.Add(d); } return View(viewModel); diff --git a/src/DotNetEd.CoreAdmin/ViewModels/DataListViewModel.cs b/src/DotNetEd.CoreAdmin/ViewModels/DataListViewModel.cs index 1ef710c..bda00d5 100644 --- a/src/DotNetEd.CoreAdmin/ViewModels/DataListViewModel.cs +++ b/src/DotNetEd.CoreAdmin/ViewModels/DataListViewModel.cs @@ -11,5 +11,6 @@ public class DataListViewModel public IEnumerable Data { get; internal set; } public DbContext DbContext { get; internal set; } public PropertyInfo DbSetProperty { get; internal set; } + public string DbDisplayName { get; internal set; } } } diff --git a/src/DotNetEd.CoreAdmin/ViewModels/MenuViewModel.cs b/src/DotNetEd.CoreAdmin/ViewModels/MenuViewModel.cs index 4646caf..afb87fa 100644 --- a/src/DotNetEd.CoreAdmin/ViewModels/MenuViewModel.cs +++ b/src/DotNetEd.CoreAdmin/ViewModels/MenuViewModel.cs @@ -6,5 +6,7 @@ public class MenuViewModel { public List DbContextNames { get; set; } = new List(); public List DbSetNames { get; set; } = new List(); + + public List DbDisplayNames { get; set; } = new List(); } } diff --git a/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Create.cshtml b/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Create.cshtml index a07fab6..a9b8e21 100644 --- a/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Create.cshtml +++ b/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Create.cshtml @@ -6,7 +6,7 @@ Layout = "_CoreAdminLayout"; } -

@ViewBag.DbSetName - @_localizer["Create"]

+

@ViewBag.DbDisplayName - @_localizer["Create"]

@using (Html.BeginForm("CreateEntityPost", diff --git a/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Edit.cshtml b/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Edit.cshtml index 579f6be..5d70814 100644 --- a/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Edit.cshtml +++ b/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Edit.cshtml @@ -6,7 +6,7 @@ Layout = "_CoreAdminLayout"; } -

@ViewBag.DbSetName - @_localizer["Edit"]

+

@ViewBag.DbDisplayName - @_localizer["Edit"]

@using (Html.BeginForm("EditEntityPost", diff --git a/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Index.cshtml b/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Index.cshtml index ae976f5..c460756 100644 --- a/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Index.cshtml +++ b/src/DotNetEd.CoreAdmin/Views/CoreAdminData/Index.cshtml @@ -16,7 +16,7 @@ var options = CoreAdminOptions.FirstOrDefault(); } -

@Model.DbSetProperty.Name

+

@Model.DbDisplayName

@@ -24,45 +24,55 @@
-@(Html +@( +Html .Grid(Model.Data) .Build((columns) => { foreach(var entityProperty in Model.EntityType.GetProperties()) { - ParameterExpression entity = Expression.Parameter(typeof(object), "ent"); var changedType = Expression.Convert(entity, Model.EntityType); var property = Expression.Property(changedType, entityProperty.Name); var propertyDisplayName = entityProperty.Name; - if(Attribute.IsDefined(entityProperty, typeof(DisplayAttribute))) + + + if (Attribute.IsDefined(entityProperty, typeof(DisplayAttribute))) { - var displayAttribute = entityProperty.GetCustomAttributes(typeof(DisplayAttribute),true).FirstOrDefault() as DisplayAttribute; - if(displayAttribute != null) + var displayAttribute = entityProperty.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute; + if (displayAttribute != null) { var displayAutoGeneratedField = displayAttribute.GetAutoGenerateField(); - if(!( displayAutoGeneratedField.HasValue? displayAutoGeneratedField.Value : false )) + if (!(displayAutoGeneratedField.HasValue ? displayAutoGeneratedField.Value : false)) continue; - if(!string.IsNullOrEmpty(displayAttribute.Description)) - propertyDisplayName = displayAttribute.Description; + if (!string.IsNullOrEmpty(displayAttribute.Name)) + propertyDisplayName = displayAttribute.Name; } - } - else if (entityProperty.PropertyType == typeof(Boolean)) + } + + if (entityProperty.PropertyType == typeof(Boolean)) { - var lambda = Expression.Lambda>(property, entity); - columns.Add(lambda).RenderedAs(m=> Html.CheckBox(entityProperty.Name, lambda.Compile().Invoke(m))).Titled(propertyDisplayName); + var l = Expression.Lambda>(property, entity); + columns.Add(l).RenderedAs(m => Html.CheckBox(entityProperty.Name, l.Compile().Invoke(m), new { onclick = "return false;", style = "cursor: default;" })).Titled(propertyDisplayName); + continue; } - else if (entityProperty.PropertyType == typeof(byte[])) + + + if (entityProperty.PropertyType == typeof(byte[])) { - var lambda = Expression.Lambda>(property, entity); + var l = Expression.Lambda>(property, entity); - columns.Add(lambda).Titled(entityProperty.Name) - .RenderedAs((value) => { - var base64 = ImageUtils.WebBase64EncodeImageByteArrayOrNull(lambda.Compile().Invoke(value)); + columns.Add(l).Titled(entityProperty.Name) + .RenderedAs((value) => + { + var base64 = ImageUtils.WebBase64EncodeImageByteArrayOrNull(l.Compile().Invoke(value)); return base64 == null ? string.Empty : $""; - } ).Encoded(false); - } - /* if (entityProperty.PropertyType == typeof(Guid)) + }).Encoded(false); + continue; + } + + + /* if (entityProperty.PropertyType == typeof(Guid)) { var lambda = Expression.Lambda>(property, entity); @@ -128,23 +138,22 @@ var lambda = Expression.Lambda>(property, entity); columns.Add(lambda).Titled(propertyDisplayName); }*/ - else - { - //Use reflection to build up lambda expression and call columns.Add(...) and Titled(...) for any field that can be rendered as text. - //This is equivalent to: - //if(entityProperty.PropertyType == typeof(Foo?)){ - // var lambda = Expression.Lambda>(property, entity); - // columns.Add(lambda).Titled(propertyDisplayName); - //} - //But avoids needing an else if for each possible type, nullable types, etc.. - var funcType = typeof(Func<,>).MakeGenericType(typeof(object),entityProperty.PropertyType); - var lambdaMethod = typeof(Expression).GetMethods(BindingFlags.Public|BindingFlags.Static).Where(m=>m.Name == "Lambda" && m.IsGenericMethod).First().MakeGenericMethod(funcType); - var lambda = lambdaMethod.Invoke(null,new object[]{property,new ParameterExpression[]{entity}}); - var addMethod = columns.GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance).Where(m=>m.Name=="Add" && m.IsGenericMethod).First(); - var column = addMethod.MakeGenericMethod(entityProperty.PropertyType).Invoke(columns,new object[]{lambda}); - var titledMethod = typeof(GridColumnExtensions).GetMethods(BindingFlags.Static|BindingFlags.Public).Where(m=>m.Name=="Titled").First().MakeGenericMethod(typeof(object),entityProperty.PropertyType); - titledMethod.Invoke(null, new[]{column, propertyDisplayName}); - } + + //Use reflection to build up lambda expression and call columns.Add(...) and Titled(...) for any field that can be rendered as text. + //This is equivalent to: + //if(entityProperty.PropertyType == typeof(Foo?)){ + // var lambda = Expression.Lambda>(property, entity); + // columns.Add(lambda).Titled(propertyDisplayName); + //} + //But avoids needing an else if for each possible type, nullable types, etc.. + var funcType = typeof(Func<,>).MakeGenericType(typeof(object),entityProperty.PropertyType); + var lambdaMethod = typeof(Expression).GetMethods(BindingFlags.Public|BindingFlags.Static).Where(m=>m.Name == "Lambda" && m.IsGenericMethod).First().MakeGenericMethod(funcType); + var lambda = lambdaMethod.Invoke(null,new object[]{property,new ParameterExpression[]{entity}}); + var addMethod = columns.GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance).Where(m=>m.Name=="Add" && m.IsGenericMethod).First(); + var column = addMethod.MakeGenericMethod(entityProperty.PropertyType).Invoke(columns,new object[]{lambda}); + var titledMethod = typeof(GridColumnExtensions).GetMethods(BindingFlags.Static|BindingFlags.Public).Where(m=>m.Name=="Titled").First().MakeGenericMethod(typeof(object),entityProperty.PropertyType); + titledMethod.Invoke(null, new[]{column, propertyDisplayName}); + } // only supports single PKs, not composite ones diff --git a/src/DotNetEd.CoreAdmin/Views/Shared/Components/CoreAdminMenu/Default.cshtml b/src/DotNetEd.CoreAdmin/Views/Shared/Components/CoreAdminMenu/Default.cshtml index 14d2c04..704f099 100644 --- a/src/DotNetEd.CoreAdmin/Views/Shared/Components/CoreAdminMenu/Default.cshtml +++ b/src/DotNetEd.CoreAdmin/Views/Shared/Components/CoreAdminMenu/Default.cshtml @@ -1,4 +1,5 @@ -@model DotNetEd.CoreAdmin.ViewModels.MenuViewModel + +@model DotNetEd.CoreAdmin.ViewModels.MenuViewModel