diff --git a/Mvc.JQuery.Datatables.Example/App_Start/RegisterDatatablesModelBinder.cs b/Mvc.JQuery.Datatables.Example/App_Start/RegisterDatatablesModelBinder.cs index fe7647b..ffb88f5 100644 --- a/Mvc.JQuery.Datatables.Example/App_Start/RegisterDatatablesModelBinder.cs +++ b/Mvc.JQuery.Datatables.Example/App_Start/RegisterDatatablesModelBinder.cs @@ -7,7 +7,7 @@ namespace Mvc.JQuery.Datatables.Example.App_Start { public static class RegisterDatatablesModelBinder { public static void Start() { - ModelBinders.Binders.Add(typeof(Mvc.JQuery.Datatables.DataTablesParam), new Mvc.JQuery.Datatables.DataTablesModelBinder()); + ModelBinders.Binders.Add(typeof(DataTablesParam), new DataTablesModelBinder()); } } } diff --git a/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs b/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs index cd8f60e..20ac175 100644 --- a/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs +++ b/Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.ComponentModel; using System.Globalization; using System.Linq; using System.Web; @@ -39,6 +40,7 @@ public DataTablesResult GetUsers(DataTablesParam dataTableParam) Hired = user.Hired }); } + public DataTablesResult GetUsersUntyped(DataTablesParam dataTableParam) { var users = Users(); @@ -79,6 +81,7 @@ public enum Numbers public class User { public int Id { get; set; } + public string Name { get; set; } public string Email { get; set; } @@ -91,7 +94,9 @@ public class User public class UserView { - public int Id { get; set; } + public int Id { get; set; } + + [DisplayName("Full Name")] public MvcHtmlString Name { get; set; } public string Email { get; set; } diff --git a/Mvc.JQuery.Datatables.Example/Mvc.JQuery.Datatables.Example.csproj b/Mvc.JQuery.Datatables.Example/Mvc.JQuery.Datatables.Example.csproj index 5aaa57b..cdcee74 100644 --- a/Mvc.JQuery.Datatables.Example/Mvc.JQuery.Datatables.Example.csproj +++ b/Mvc.JQuery.Datatables.Example/Mvc.JQuery.Datatables.Example.csproj @@ -1,267 +1,278 @@ - - - - Debug - AnyCPU - - - 2.0 - {476D3266-8699-4E89-A528-646C1235EEB1} - {E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} - Library - Properties - Mvc.JQuery.Datatables.Example - Mvc.JQuery.Datatables.Example - v4.0 - false - false - - - true - full - false - bin\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\ - TRACE - prompt - 4 - - - - ..\packages\EmbeddedResourceVirtualPathProvider.1.2.5\lib\net40\EmbeddedResourceVirtualPathProvider.dll - - - True - ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll - - - - - - - - - - - - - - - - - - - - - - - - - - ..\packages\WebActivator.1.5\lib\net40\WebActivator.dll - - - - - - - - Global.asax - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Web.config - - - Web.config - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {389ADE94-7C32-4885-812E-68A2A74C82D8} - Mvc.JQuery.Datatables - - - - + + + + + Debug + AnyCPU + + + 2.0 + {476D3266-8699-4E89-A528-646C1235EEB1} + {E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + Mvc.JQuery.Datatables.Example + Mvc.JQuery.Datatables.Example + v4.0 + false + false + + + + + 4.0 + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + ..\packages\EmbeddedResourceVirtualPathProvider.1.2.5\lib\net40\EmbeddedResourceVirtualPathProvider.dll + + + True + ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + + + + + + + + + + + + + + + + + + + + + + + + + + ..\packages\WebActivator.1.5\lib\net40\WebActivator.dll + + + + + + + + Global.asax + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Web.config + + + Web.config + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {389ADE94-7C32-4885-812E-68A2A74C82D8} + Mvc.JQuery.Datatables + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + - - - - - - - - False - True - 50564 - / - - - False - False - - - False - - - - + --> + + + + + + + + False + True + 50564 + / + + + False + False + + + False + + + + \ No newline at end of file diff --git a/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml b/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml index ddf0cde..2627441 100644 --- a/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml +++ b/Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml @@ -8,7 +8,7 @@ @foreach (var column in Model.Columns) { - @column.Item1 + @column.Item2 } @if (Model.ColumnFilter) @@ -16,7 +16,7 @@ @foreach (var column in Model.Columns) { - @column.Item1 + @column.Item2 } } diff --git a/Mvc.JQuery.Datatables.sln b/Mvc.JQuery.Datatables.sln index 01f0a12..87aa078 100644 --- a/Mvc.JQuery.Datatables.sln +++ b/Mvc.JQuery.Datatables.sln @@ -1,32 +1,32 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mvc.JQuery.Datatables", "Mvc.JQuery.Datatables\Mvc.JQuery.Datatables.csproj", "{389ADE94-7C32-4885-812E-68A2A74C82D8}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mvc.JQuery.Datatables.Example", "Mvc.JQuery.Datatables.Example\Mvc.JQuery.Datatables.Example.csproj", "{476D3266-8699-4E89-A528-646C1235EEB1}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mvc.JQuery.Datatables.Templates", "Mvc.JQuery.Datatables.Templates\Mvc.JQuery.Datatables.Templates.csproj", "{8DA3D5E6-D714-40C3-B6CB-E88D852C5175}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {389ADE94-7C32-4885-812E-68A2A74C82D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {389ADE94-7C32-4885-812E-68A2A74C82D8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {389ADE94-7C32-4885-812E-68A2A74C82D8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {389ADE94-7C32-4885-812E-68A2A74C82D8}.Release|Any CPU.Build.0 = Release|Any CPU - {476D3266-8699-4E89-A528-646C1235EEB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {476D3266-8699-4E89-A528-646C1235EEB1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {476D3266-8699-4E89-A528-646C1235EEB1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {476D3266-8699-4E89-A528-646C1235EEB1}.Release|Any CPU.Build.0 = Release|Any CPU - {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mvc.JQuery.Datatables", "Mvc.JQuery.Datatables\Mvc.JQuery.Datatables.csproj", "{389ADE94-7C32-4885-812E-68A2A74C82D8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mvc.JQuery.Datatables.Example", "Mvc.JQuery.Datatables.Example\Mvc.JQuery.Datatables.Example.csproj", "{476D3266-8699-4E89-A528-646C1235EEB1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mvc.JQuery.Datatables.Templates", "Mvc.JQuery.Datatables.Templates\Mvc.JQuery.Datatables.Templates.csproj", "{8DA3D5E6-D714-40C3-B6CB-E88D852C5175}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {389ADE94-7C32-4885-812E-68A2A74C82D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {389ADE94-7C32-4885-812E-68A2A74C82D8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {389ADE94-7C32-4885-812E-68A2A74C82D8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {389ADE94-7C32-4885-812E-68A2A74C82D8}.Release|Any CPU.Build.0 = Release|Any CPU + {476D3266-8699-4E89-A528-646C1235EEB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {476D3266-8699-4E89-A528-646C1235EEB1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {476D3266-8699-4E89-A528-646C1235EEB1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {476D3266-8699-4E89-A528-646C1235EEB1}.Release|Any CPU.Build.0 = Release|Any CPU + {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8DA3D5E6-D714-40C3-B6CB-E88D852C5175}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Mvc.JQuery.Datatables/DataTableVm.cs b/Mvc.JQuery.Datatables/DataTableVm.cs index ca2f97d..47ce538 100644 --- a/Mvc.JQuery.Datatables/DataTableVm.cs +++ b/Mvc.JQuery.Datatables/DataTableVm.cs @@ -14,7 +14,7 @@ static DataTableVm() public static string DefaultTableClass { get; set; } public string TableClass { get; set; } - public DataTableVm(string id, string ajaxUrl, IEnumerable> columns) + public DataTableVm(string id, string ajaxUrl, IEnumerable> columns) { AjaxUrl = ajaxUrl; this.Id = id; @@ -32,7 +32,7 @@ public DataTableVm(string id, string ajaxUrl, IEnumerable> c public string AjaxUrl { get; private set; } - public IEnumerable> Columns { get; private set; } + public IEnumerable> Columns { get; private set; } public bool ColumnFilter { get; set; } @@ -42,7 +42,7 @@ public DataTableVm(string id, string ajaxUrl, IEnumerable> c public string ColumnFiltersString { - get { return string.Join(",", Columns.Select(c => GetFilterType(c.Item1, c.Item2))); } + get { return string.Join(",", Columns.Select(c => GetFilterType(c.Item1, c.Item3))); } } public string Dom diff --git a/Mvc.JQuery.Datatables/DataTablesFilter.cs b/Mvc.JQuery.Datatables/DataTablesFilter.cs index 4b10977..880c09d 100644 --- a/Mvc.JQuery.Datatables/DataTablesFilter.cs +++ b/Mvc.JQuery.Datatables/DataTablesFilter.cs @@ -7,7 +7,7 @@ namespace Mvc.JQuery.Datatables { public class DataTablesFilter { - public IQueryable FilterPagingSortingSearch(DataTablesParam dtParameters, IQueryable data, out int totalRecordsDisplay, Tuple[] columns) + public IQueryable FilterPagingSortingSearch(DataTablesParam dtParameters, IQueryable data, out int totalRecordsDisplay, Tuple[] columns) { if (!String.IsNullOrEmpty(dtParameters.sSearch)) { @@ -156,11 +156,11 @@ public static void RegisterFilter(GuardedFilter filter) Filters.Add(Guard(Is, filter)); } - private static string GetFilterClause(string query, Tuple column, List parametersForLinqQuery) + private static string GetFilterClause(string query, Tuple column, List parametersForLinqQuery) { foreach (var filter in Filters) { - var filteredQuery = filter(query, column.Item1, column.Item2, parametersForLinqQuery); + var filteredQuery = filter(query, column.Item1, column.Item3, parametersForLinqQuery); if (filteredQuery != null) { return filteredQuery; diff --git a/Mvc.JQuery.Datatables/DataTablesHelper.cs b/Mvc.JQuery.Datatables/DataTablesHelper.cs index 5b32487..aa4b343 100644 --- a/Mvc.JQuery.Datatables/DataTablesHelper.cs +++ b/Mvc.JQuery.Datatables/DataTablesHelper.cs @@ -1,7 +1,9 @@ using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.ComponentModel; using System.Linq; -using System.Linq.Expressions; +using System.Linq.Expressions; +using System.Reflection; using System.Text; using System.Web; using System.Web.Mvc; @@ -31,11 +33,25 @@ public static IHtmlString DataTableIncludes(this HtmlHelper helper, bool jqueryU } - public static DataTableVm DataTableVm(this HtmlHelper html, string id, Expression>> exp, params Tuple[] columns) + public static DataTableVm DataTableVm(this HtmlHelper html, string id, Expression>> exp, params Tuple[] columns) { - if (columns == null || columns.Length == 0) - { - columns = typeof(TResult).GetProperties().Where(p => p.GetGetMethod() != null).Select(p => Tuple.Create(p.Name, p.PropertyType)).ToArray(); + if (columns == null || columns.Length == 0) + { + var propInfos = typeof (TResult).GetProperties().Where(p => p.GetGetMethod() != null).ToList(); + var columnList = new List>(); + foreach (var propertyInfo in propInfos) + { + var displayNamettribute = (DisplayNameAttribute)propertyInfo.GetCustomAttributes(typeof (DisplayNameAttribute), false).FirstOrDefault(); + if(displayNamettribute != null) + { + columnList.Add(Tuple.Create(propertyInfo.Name, displayNamettribute.DisplayName, propertyInfo.PropertyType)); + } + else + { + columnList.Add(Tuple.Create(propertyInfo.Name, propertyInfo.Name, propertyInfo.PropertyType)); + } + } + columns = columnList.ToArray(); } var mi = exp.MethodInfo(); @@ -48,7 +64,7 @@ public static DataTableVm DataTableVm(this HtmlHelper html public static DataTableVm DataTableVm(this HtmlHelper html, string id, string ajaxUrl, params string[] columns) { - return new DataTableVm(id, ajaxUrl, columns.Select(c => Tuple.Create(c, typeof(string)))); + return new DataTableVm(id, ajaxUrl, columns.Select(c => Tuple.Create(c, (string)null, typeof(string)))); } } } \ No newline at end of file diff --git a/Mvc.JQuery.Datatables/DataTablesResult.cs b/Mvc.JQuery.Datatables/DataTablesResult.cs index 11b699d..dfe0aa5 100644 --- a/Mvc.JQuery.Datatables/DataTablesResult.cs +++ b/Mvc.JQuery.Datatables/DataTablesResult.cs @@ -51,7 +51,7 @@ public DataTablesResult(IQueryable q, DataTablesParam dataTableParam, Func Tuple.Create(p.Name, p.PropertyType)).ToArray()); + var content = GetResults(q, dataTableParam, properties.Select(p => Tuple.Create(p.Name, (string)null, p.PropertyType)).ToArray()); this.Data = content; this.JsonRequestBehavior = JsonRequestBehavior.DenyGet; } @@ -82,7 +82,7 @@ public static void RegisterFilter(GuardedValueTransformer filter) { PropertyTransformers.Add(Guard(filter)); } - private DataTablesData GetResults(IQueryable data, DataTablesParam param, Tuple[] searchColumns) + private DataTablesData GetResults(IQueryable data, DataTablesParam param, Tuple[] searchColumns) { int totalRecords = data.Count();