Skip to content

ASP.NET MVC classes to fully integrate with DataTables 1.10 and above

License

Notifications You must be signed in to change notification settings

snuxoll/datatables.mvc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Datatables.Mvc

ASP.NET MVC 5 classes to bind DataTables (1.10 and above with the new camelCase API) with your controllers.

But why another project for this?

First, it's not just another project. This one provides binding for ASP.NET MVC with DataTables 1.10.

Second, it's all about the new DataTables API (camelCase).
So far, DataTables was using the Hungarian Notation and that was, for me, a major pain. It all comes down to a developer's choice.

Finally, there was no binder, so far, for this scenario. Of course we could simply get the request parameters manually (after all, that's what the DataTables.Mvc is doing, right?) but this should help you avoid type-casting parameters, detecting, filtering and more. Also, this should help your focus on business rules instead of HTTP parameter checking.

What about demo?

It's as simple as it gets:

public ActionResult MyActionResult([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
{
    // do your stuff...
	var paged = myFilteredData.Skip(requestModel.Start).Take(requestModel.Length);
    return View(new DataTablesResponse(requestModel.Draw, paged, myFilteredData.Count(), myOriginalDataSet.Count()));
}

// Or if you'd like to return a JsonResult, try this:

public JsonResult MyActionResult([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
{
    // do your stuff...
	var paged = myFilteredData.Skip(requestModel.Start).Take(requestModel.Length);
	return Json(new DataTablesResponse(requestModel.Draw, paged, myFilteredData.Count(), myOriginalDataSet.Count()));
}

What about filtering/ordering?

It's a no brainer too.

Filter/sort info from each column is, well, included on each column.

To help you out, there are two methods on ColumnCollection:
IDataTablesRequest.Columns.GetSortedColumns() will return an ordered enumeration of sorted columns.
IDataTablesRequest.Columns.GetFilteredColumns() will return an enumeration of columns which were actually filtered on client-side.

Sample:

```C# // Apply filter to your dataset based only on the columns that actually have a search value. var filteredColumns = requestParameters.Columns.GetFilteredColumns(); foreach(var column in filteredColumns) Filter(column.Data, column.Search.Value, column.Search.IsRegexValue);

// Set your dataset on the same order as requested from client-side either directly on your SQL code or easily // into any type or enumeration. var sortedColumns = requestParameters.Columns.GetSortedColumns(); var isSorted = false; foreach(var column in sortedColumns) { if (!isSorted) { Sort(column.Data, column.SortDirection); isSorted = true; } else { SortAgain(column.Data, column.SortDirection); } }

<h3>Is it possible to add custom parameters sent with my request?</h3>
<p>
	Sure! It's a piece of cake now. Override <code>BindModel</code> and make it call <code>Bind</code> with your custom implementation of <code>IDataTablesRequest</code>.<br />
	Than, override the <code>MapAditionalProperties</code> to map your extra info into your custom type.<br />
	Here's a sample:
</p>
```C#
// Create a custom type from DataTablesBinder.
public class MyBinder : DataTablesBinder
{
    // Override the default BindModel called by ASP.NET and make it call Bind passing the type of your
    // implementation of IDataTablesRequest:
    public override object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
    {
        return Bind(controllerContext, bindingContext, typeof(MyCustomRequest));
    }
	
	// Override MapAditionalProperties so you can set your aditional data into the model:
    protected override void MapAditionalColumns(IDataTablesRequest requestModel, System.Collections.Specialized.NameValueCollection requestParameters)
    {
            var myModel = (MyCustomRequest)requestModel;
            myModel.MyCustomProp = Get<string>(requestParameters, "myCustomProp");
    }
}

// You'll need a custom request model, of course.
// Just derive from DefaultDataTablesRequest and you're fine :)
// You can choose to implement IDataTablesRequest too, if you like.
public class MyCustomRequest : DefaultDataTablesRequest
{
    public string MyCustomProp { get; set; }
}

// Than, on your controller/action, decorate with:
public ActionResult MyActionResult([ModelBinder(typeof(MyBinder))] MyCustomRequest requestModel)

Any issues?

If you do find any issues, please, submit then and I'll fix it ASAP.

About

ASP.NET MVC classes to fully integrate with DataTables 1.10 and above

Resources

License

Stars

Watchers

Forks

Packages