Sample implementation of serverside processing in C# MVC, EF with paging/sorting/searching

Sample implementation of serverside processing in C# MVC, EF with paging/sorting/searching

pingcrosbypingcrosby Posts: 29Questions: 4Answers: 1
edited February 2017 in Free community support

I have seen people asking a few times how to use datatables serverside with MVC and EF with paging sorting and searching etc. I had some code lying around so I thought its time to give back and share a simple implementation.

Unfortunately I don't have time right now to create a sample solution for download so I just hacked out the code and pasted it in here as a guide.

For the dynamic searching (where clause) you will need linqkit for the predicate building.

First map the datatable inbound JSON requests to classes

Start - JSon class sent from Datatables

public class DataTableAjaxPostModel
{
    // properties are not capital due to json mapping
    public int draw { get; set; }
    public int start { get; set; }
    public int length { get; set; }
    public List<Column> columns { get; set; }
    public Search search { get; set; }
    public List<Order> order { get; set; }
}

public class Column
{
    public string data { get; set; }
    public string name { get; set; }
    public bool searchable { get; set; }
    public bool orderable { get; set; }
    public Search search { get; set; }
}

public class Search
{
    public string value { get; set; }
    public string regex { get; set; }
}

public class Order
{
    public int column { get; set; }
    public string dir { get; set; }
}
/// End- JSon class sent from Datatables

Next implement your action in a standard controller (note in this example we are not using a Web-API controller)

This method just grabs the data sent from the table and calls YourCustomSearchFunc() before returning a formatted json obj for Datatables to consume.

public JsonResult CustomServerSideSearchAction(DataTableAjaxPostModel model)
{
    // action inside a standard controller
    int filteredResultsCount;
    int totalResultsCount;
    var res = YourCustomSearchFunc(model, out filteredResultsCount, out totalResultsCount);

    var result = new List<YourCustomSearchClass>(res.Count);
    foreach (var s in res)
    {
        // simple remapping adding extra info to found dataset
        result.Add(new YourCustomSearchClass
        {
            EmployerId = User.ClaimsUserId(),
            Id = s.Id,
            Pin = s.Pin,
            Firstname = s.Firstname,
            Lastname = s.Lastname,
            RegistrationStatusId = DoSomethingToGetIt(s.Id),
            Address3 = s.Address3,
            Address4 = s.Address4
        });
    };

    return Json(new
    {
        // this is what datatables wants sending back
        draw = model.draw,
        recordsTotal = totalResultsCount,
        recordsFiltered = filteredResultsCount,
        data = result
    });
}

YourCustomSearchFunc() is very simple it just sets up the sort column and sort direction before calling the database search functionality. In this example we are only allowing sorting on a single column but you could easily implement multi column sorting.

public IList<YourCustomSearchClass> YourCustomSearchFunc(DataTableAjaxPostModel model, out int filteredResultsCount, out int totalResultsCount)
{
    var searchBy = (model.search != null) ? model.search.value : null;
    var take = model.length;
    var skip = model.start;

    string sortBy = "";
    bool sortDir = true;

    if (model.order != null)
    {
        // in this example we just default sort on the 1st column 
        sortBy = model.columns[model.order[0].column].data;
        sortDir = model.order[0].dir.ToLower() == "asc";
    }

    // search the dbase taking into consideration table sorting and paging
    var result = GetDataFromDbase(searchBy, take, skip, sortBy, sortDir, out filteredResultsCount, out totalResultsCount);
    if (result == null)
    {
        // empty collection...
        return new List<YourCustomSearchClass>();
    }
    return result;
}

This is the main meat of the functionality. In it we simply select from the dbase but instead of using a fixed where clause we use a dynamic expression built using the wonderful LinqKit to generate the predicate.

Additionally we use Take and Skip to allow us to page through the data. Notice we use the where clause twice. Once to select the data and pick a page, the second time to count how many items we could have returned.

public List<YourCustomSearchClass> GetDataFromDbase(string searchBy, int take, int skip, string sortBy, bool sortDir, out int filteredResultsCount, out int totalResultsCount)
{
    // the example datatable used is not supporting multi column ordering 
    // so we only need get the column order from the first column passed to us.         
    var whereClause = BuildDynamicWhereClause(Db, searchBy);

    if (String.IsNullOrEmpty(searchBy))
    {
        // if we have an empty search then just order the results by Id ascending
        sortBy = "Id";
        sortDir = true;
    }

    var result = Db.DatabaseTableEntity
                   .AsExpandable()
                   .Where(whereClause)
                   .Select(m => new YourCustomSearchClass
                   {
                       Id = m.Id,
                       Firstname = m.Firstname,
                       Lastname = m.Lastname,
                       Address1 = m.Address1,
                       Address2 = m.Address2,
                       Address3 = m.Address3,
                       Address4 = m.Address4,
                       Phone = m.Phone,                      
                       Postcode = m.Postcode,                      
                   })
                   .OrderBy(sortBy, sortDir) // have to give a default order when skipping .. so use the PK
                   .Skip(skip)
                   .Take(take)
                   .ToList();

    // now just get the count of items (without the skip and take) - eg how many could be returned with filtering
    filteredResultsCount = Db.DatabaseTableEntity.AsExpandable().Where(whereClause).Count();
    totalResultsCount = Db.DatabaseTableEntity.Count();

    return result;
}

Here is the predicate builder function that just plugs in a where clause dynamically. You will need to install (nugget) in linqkit for this.

In this example I am searching where the searchterm appears in either the firstname or lastname

private Expression<Func<DatabaseTableMappedClass, bool>> BuildDynamicWhereClause(DBEntities entities, string searchValue)
{
    // simple method to dynamically plugin a where clause 
    var predicate = PredicateBuilder.New<DatabaseTableMappedClass>(true); // true -where(true) return all
    if (String.IsNullOrWhiteSpace(searchValue) == false)
    {
        // as we only have 2 cols allow the user type in name 'firstname lastname' then use the list to search the first and last name of dbase
        var searchTerms = searchValue.Split(' ').ToList().ConvertAll(x => x.ToLower());

        predicate = predicate.Or(s => searchTerms.Any(srch => s.Firstname.ToLower().Contains(srch)));
        predicate = predicate.Or(s => searchTerms.Any(srch => s.Lastname.ToLower().Contains(srch)));
    }
    return predicate;
}

The only left to show is the datatable itself

var table = $('#SearchResultTable').DataTable({
    "proccessing": true,
    "serverSide": true,
    "ajax": {
        url: "@Url.Action("CustomServerSideSearchAction", "Home")",
        type: 'POST'
    },
    "language": {
        "search": "",
        "searchPlaceholder": "Search..."
    },
   "columns": [
        { "data": "Firstname" },
        { "data": "Lastname" }
    ]
});

Hope this helps

Replies

  • allanallan Posts: 48,464Questions: 1Answers: 7,020 Site admin

    Superb - bookmarked! Thanks for sharing this. Hugely appreciated!

    Allan

  • joromajoroma Posts: 2Questions: 0Answers: 0

    Aside from LinqKit, did you use any other library? How did you get OrderBy to accept string parameters?

  • jhomel89jhomel89 Posts: 1Questions: 0Answers: 0

    Thanks for the source codes. You save my day. But please modify the the 'ajax' portion in the js file. it should be like this:

    "ajax": {
    url: "/Controller/Action", //somthing like this @Url.Action("CustomServerSideSearchAction", "Home")
    type: 'POST',
    contentType: "application/json",
    data: function (d) {
    return JSON.stringify(d);
    }
    }
    we need to stringify the data before sending it to controller action to bind it to the model.

  • erensoguterensogut Posts: 1Questions: 0Answers: 0
    edited November 2017

    Thanks for the codes.But I can not handle how pass to length of model via ajax.Because I made a filtering modal and remove original X entries per page part.I need pass it via model above.

  • Aquib_RaitechAquib_Raitech Posts: 1Questions: 0Answers: 0

    Thanks for implementation, saved my time. But one thing how to load next page data, because it just sorting the data from server side and loading in the first page, but how can i get second page data or other relative pages data just like normal datatable works.

  • filippoacetofilippoaceto Posts: 4Questions: 0Answers: 0

    i can't load next page data, it remain processing...
    But in the server the data is loaded correctly.

  • SylvanovSylvanov Posts: 1Questions: 0Answers: 0

    Hi Everybody,

    sorry to come here very late. But later than never. I've tried this and it working I'll not says perfectly cause I had to change a code to make id work.
    this is the code I changed : In methode "GetDataFromDbase" I change the "OrderBy(sortBy, sortDir)" into a normal OrderBy, thus I'm unable to sort oder columns.

    How can I fixe this? any solution will be verry appreciated.

  • MsilviuMsilviu Posts: 1Questions: 0Answers: 0
    edited February 5

    @Sylvanov you can use the following extension method:
    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string memberName, bool asc = true)
    {
    ParameterExpression[] typeParams = new ParameterExpression[] {Expression.Parameter(typeof(T), "")};

            System.Reflection.PropertyInfo pi = typeof(T).GetProperty(memberName);
    
            return (IOrderedQueryable<T>) query.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable),
                    asc ? "OrderBy" : "OrderByDescending",
                    new Type[] {typeof(T), pi.PropertyType},
                    query.Expression,
                    Expression.Lambda(Expression.Property(typeParams[0], pi), typeParams))
                );
        }
    
  • goldharborgoldharbor Posts: 1Questions: 0Answers: 0

    Hello, there is any implementation with additional parameters in the Model, like a Dictionary?

  • NandsNands Posts: 2Questions: 0Answers: 0

    Hi, I got this working with MVC 5 but when tried with MVC 4, the DataTableAjaxPostModel object comes into the controller method but model.search.value is null.
    Any idea about the cause or the starting point to debug it?

  • NandsNands Posts: 2Questions: 0Answers: 0

    Adding
    contentType: "application/json",
    data: function (d) {
    return JSON.stringify(d);
    }
    resolved the issue.

  • forsythwtforsythwt Posts: 1Questions: 0Answers: 0

    For those who visit this article in the future and are trying to do this with .net core, let me assist you, and save some of your hair and sanity.

    In the CustomServerSideSearchAction, you need to add the attribute [FromBody] to the incoming parameter. This is because .net core doesn't read the content-type of incoming json objects by default.

    //Source:
    https://andrewlock.net/model-binding-json-posts-in-asp-net-core/

  • jessicamjessicam Posts: 1Questions: 0Answers: 0

    You are a hero.

  • eal2100eal2100 Posts: 3Questions: 1Answers: 1

    In case anyone has trouble using the OrderBy, try downloading the NuGet for System.Linq.Dynamic.

  • Mohitgupta123Mohitgupta123 Posts: 2Questions: 0Answers: 0

    I can't understand when click on page number than how to hit controller and which parameter is send by clicking on that page number.

    Note- I just want to know how to send parameter like page number ,limit and display count to the controller in Spring MVC.

    If some one implements client side pagination than please provide that example

    Mail_id-- guptamohit.psit@gmail.com
    skype_id- guptamohit.psit@gmail.com

    Thanks in advance.

  • CupOfJoeCupOfJoe Posts: 1Questions: 0Answers: 0

    You're a F$#%#$ godsend. As someone who is new to this, I've been banging my head against the wall for a day or so, until now. Thank you for the post!

Sign In or Register to comment.