mrplume
mrplume

Reputation: 183

Asp .NET MVC sort multi criteria with ajax/json

My goal is to built in my controller a multi-criteria system for my activity session view. I have three way of filtering : By keyword, by start/end date and by couple of activity (log in / log off, lock on / lock off...). An user could use none, one or all's of them.

There is this post : How to structure controller to sort multiple criteria asp.net mvc , which is very close of my needs, but it was anwered in 2010, could you tell me if it up to date ? is there a more simple solution ?

For the moment I have built each criteria separatly with ajax/json/js. It's working, but I can't cumulate them.

//----Filter-----Date Period------
$("#btndatefilter").click(function () {
    var startdate = $('#datestart').datepicker('getDate');
    var enddate = $('#dateend').datepicker('getDate');
    var dateFilter ={ StartDate: startdate.toISOString(), EndDate: enddate.toISOString() };
    $.ajax({
        url: "/AuditActivities/FilterByDate",
        type: "GET",
        data: dateFilter,
    })
    .done(function (auditActivity) {
        $("#res").html(auditActivity);
    });
});
//----Filter-----Activity------
$("#Activity").change(function () {
    var index = $('#Activity').find('option:selected').val();
    $.ajax({
        url: "/AuditActivities/FilterByActivity",
        type: "GET",
        data: { chx: index}
    })
    .done(function (auditActivity) {
        $("#res").html(auditActivity);
    });
});

And in the controller

public ActionResult FilterByActivity(int chx)
{
    string returnpartial = "";
    var auditActivity = db.AuditActivity.Include(a => a.Pc).Include(a => a.Activity).Include(a => a.Users);
    switch (chx)
    {
        case 1:
            auditActivity = auditActivity.Where(a => a.ActivityId==1 || a.ActivityId == 2);
            returnpartial = "Indexactivityuser";
            break;
        case 2:
            auditActivity = auditActivity.Where(a => a.ActivityId==3 || a.ActivityId == 4);
            returnpartial = "Indexactivityuser";
            break;
        case 3:
            auditActivity = auditActivity.Where(a => a.ActivityId==5 || a.ActivityId == 6);
            returnpartial = "Indexactivityuser";
            break;
    }
    return PartialView(returnpartial, auditActivity.ToList());
}

public ActionResult FilterByDate(DateFilter dateFilter)
{
    DateTime endperiod = dateFilter.EndDate.AddDays(1).AddTicks(-1);
    string returnpartial = "";
    var auditActivity = db.AuditActivity.Include(a => a.Pc).Include(a => a.Activity).Include(a => a.Users);
    auditActivity = auditActivity.Where(a => a.Date > dateFilter.StartDate && a.Date < endperiod).OrderBy(a => a.Date);
    returnpartial = "Indexactivityuser";
    return PartialView(returnpartial, auditActivity.ToList());
}

Upvotes: 0

Views: 180

Answers (1)

stevenrcfox
stevenrcfox

Reputation: 1567

To put these together, you will need to submit them as a single request.

Something like the following:

public class SearchDefinition
{
     DateFilter dateFilter {get;set;}
     int activityType { get;set;}  //this is chx
}

Then modify your controller signature as follows:

public ActionResult FilterActivities(SearchDefinition searchDefn)

In implementing the controller, you've got all the concepts already in place, so I'll just illustrate an example here:

returnpartial = "Indexactivityuser";
var auditActivity = db.AuditActivity.Include(a => a.Pc).Include(a => a.Activity).Include(a => a.Users);
switch (searchDefn.activityType) //activitytype will be zero if not set in javascript
{
    case 1:
        auditActivity = auditActivity.Where(a => a.ActivityId==1 || a.ActivityId == 2);
        break;
    case 2:
        auditActivity = auditActivity.Where(a => a.ActivityId==3 || a.ActivityId == 4);
        break;
    case 3:
        auditActivity = auditActivity.Where(a => a.ActivityId==5 || a.ActivityId == 6);
        break;
}

if(searchDefn.dateFilter!=null){  // datefilter will be null if not set by javascript

    DateTime endperiod = dateFilter.EndDate.AddDays(1).AddTicks(-1);

    auditActivity = auditActivity.Where(a => a.Date > dateFilter.StartDate && a.Date < endperiod).OrderBy(a => a.Date);
}

Again you have all the concepts in the javascript, simply gather all the data behind one button and submit it in a single ajax request

Upvotes: 1

Related Questions