JQuery Datatable with MVC 5 and Entity Framework

I need some guidance on what to add to the controller so that I can use server-side processing with my jQuery data. I am using MVC 5 and Entity Framework.

Example: http://datatablesmvc.codeplex.com/documentation states the following:

public class HomeController : Controller { [HttpPost] public ActionResult GetDataTables(DataTable dataTable) { List<List<string>> table = new List<List<string>>(); //Do something with dataTable and fill table return new DataTableResult(dataTable, table.Count, table.Count, table); } } 

But what should I do when I use LINQ, for example?

  public ActionResult Index() { var activity = db.Activity.Include(a => a.ActivityType); return View(activity.ToList()); } 
+7
ajax linq asp.net-mvc entity-framework datatables
source share
1 answer

------------------------------- Updated answer -------------- --- --------------

Why update?

This answer seems to be getting more and more attention from SO users, and I thought that everyone could benefit from the β€œsmall” update.

What has changed so far?

DataTables.Mvc started over a year ago. It has changed, and now it is called DataTables.AspNet . But that's not all.

At that time, the goal was to help with the base classes. The problem is that you just get the zip code and have to manually combine all this into your project. In addition, there was no binder for the models, and the integration was really boring.

We now have a modular architecture with Nuget packages. You can either refer to the Core package, or implement everything yourself, or you can get the appropriate packages ( Mvc5 or AspNet ; WebApi2 will appear soon) with native model bindings, single-line registration and a full set of tests.

How to start?

The samples folder on the dev branch opens ( click here ). Remember to get the appropriate Nuget packages. You can find their list here .

------------------------------- Original answer -------------- --- --------------

First things first

You can use DataTables 1.9, 1.10 with the old API or 1.10 with the new API.

If you choose a new API (only 1.10), you'll skip some plugins here and there, but you can use DataTables.AspNet on GitHub to help with the bindings.

If not, you can take a look and change the code to match request variables from other versions (support will be provided later in my project).

Real deal

The point is that you have to handle three elements:

  • Global filter / search
  • Column Filter / Search
  • Column Sort

Give the code!

This may change from which version and if you use (or not) my binding class. Think that you are using it to avoid processing request parameters here, ok?

So you can play with something like this:

 [HttpPost] public ActionResult Index([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestParameters) { var totalCount = myDbContext.Set<Something>().Count(); var filteredDataSet = myDbContext.Set<Something>().Where(_s => _s.ToLower().Contains(requestParameters.Search.Value)); foreach(var column in requestParameters.Columns.GetFilteredColumns()) { // Apply individual filters to each column. // You can try Dynamic Linq to help here or you can use if statements. // DynamicLinq will be slower but code will be cleaner. } var isSorted = false; IOrderedEnumerable<Something> ordered = null; foreach(var column in requestParameters.Columns.GetSortedColumns()) { // If you choose to use Dynamic Linq, you can apply all sorting at once. // If not, you have to apply each sort manually, as follows. if (!isSorted) { // Apply first sort. if (column.SortDirection == Column.SortDirection.Ascendant) ordered.OrderBy(...); else ordered.OrderByDescending(...); isSorted = true; } else { if (column.SortDirection == Column.SortDirection.Ascendant) ordered.ThanBy(...); else ordered.ThanByDescending(...); } } var pagedData = ordered.Skip(requestParameters.Start).Take(requestParameters.Length); var dataTablesResult = new DataTablesResult( requestParameters.Draw, pagedData, filteredDataSet.Count(), totalCount ); return View(dataTablesResult); } 
+18
source share

All Articles