Skip to content

Commit f654067

Browse files
committed
Added ModelBinding, whoop!
1 parent cd1ff0c commit f654067

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

Mvc.JQuery.DataTables.AspNetCore.Example/Controllers/HomeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public IActionResult JSUnitTests()
8282
return View();
8383
}
8484

85-
public DataTablesResult<UserTableRowViewModel> GetUsers(DataTablesParam dataTableParam)
85+
public DataTablesResult<UserTableRowViewModel> GetUsers([FromForm] DataTablesParam dataTableParam)
8686
{
8787
return DataTablesResult.Create(FakeDatabase.Users.Select(user => new UserTableRowViewModel()
8888
{

Mvc.JQuery.DataTables.AspNetCore/DataTablesModelBinder.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,22 @@ private static T GetValue<T>(IValueProvider valueProvider, string key)
100100
: (T)valueResult.ConvertTo(typeof(T));
101101
}
102102
}
103+
104+
public class DataTablesModelBinderProvider : IModelBinderProvider
105+
{
106+
public IModelBinder GetBinder(ModelBinderProviderContext context)
107+
{
108+
if (context == null)
109+
{
110+
throw new ArgumentNullException(nameof(context));
111+
}
112+
113+
if (!context.Metadata.IsComplexType && context.Metadata.ModelType == typeof(string)) // only encode string types
114+
{
115+
return new DataTablesModelBinder();
116+
}
117+
118+
return null;
119+
}
120+
}
103121
}

Mvc.JQuery.DataTables.AspNetCore/Extensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
using Microsoft.AspNetCore.Mvc.Razor;
22
using Microsoft.Extensions.FileProviders;
33
using System;
4+
using System.Linq;
45
using System.Reflection;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Mvc.JQuery.DataTables;
58

69
namespace Microsoft.Extensions.DependencyInjection
710
{
811
public static class ConfigureServicesExtensions
912
{
1013
public static IServiceCollection AddMvcJQueryDataTables(this IServiceCollection services)
1114
{
15+
1216
var dataTablesViewModelType = typeof(Mvc.JQuery.DataTables.DataTableConfigVm).GetTypeInfo();
1317
var settings = new Mvc.JQuery.DataTables.Settings
1418
{
@@ -17,8 +21,20 @@ public static IServiceCollection AddMvcJQueryDataTables(this IServiceCollection
1721
};
1822
services.AddSingleton(settings);
1923
services.Configure<RazorViewEngineOptions>(s => s.FileProviders.Add(settings.FileProvider));
24+
services.AddMvc(options => { options.UseHtmlEncodeModelBinding(); });
25+
2026
return services;
2127
}
28+
29+
public static void UseHtmlEncodeModelBinding(this MvcOptions opts)
30+
{
31+
var binderToFind = opts.ModelBinderProviders.FirstOrDefault(x => x.GetType() == typeof(DataTablesModelBinderProvider));
32+
33+
if (binderToFind == null) return;
34+
35+
var index = opts.ModelBinderProviders.IndexOf(binderToFind);
36+
opts.ModelBinderProviders.Insert(index, new DataTablesModelBinderProvider());
37+
}
2238
}
2339
}
2440

@@ -34,7 +50,8 @@ public static IApplicationBuilder UseMvcJQueryDataTables(this IApplicationBuilde
3450
if(settings == null)
3551
{
3652
throw new InvalidOperationException("Unable to find the required services. Please add all the required services by calling 'IServiceCollection.{}' inside the call to 'ConfigureServices(...)' in the application startup code.");
37-
}
53+
}
54+
3855
app.UseStaticFiles(new StaticFileOptions
3956
{
4057
FileProvider = settings.FileProvider,

0 commit comments

Comments
 (0)