Greg
Greg

Reputation: 2690

How do I access the JQuery DataTables plugin aoData values in MVC3?

I am using MVC 3. How do I access the data sent by fnServerParams in an aoData object on the controller? Thanks

UPDATE: This is the jquery I am trying to use...

 function GenerateRows()
 {
 var serverParams = { "invoiceDate": "", "contractID": "" }

 serverParams.invoiceDate = $( "#InvoiceDate" ).val();
 serverParams.contractID = $( "#ContractID" ).val();

 $( '#invoiceRows' ).dataTable( {

    // Table style
    "bPaginate": false,
    "bLengthChange": false,
    "bSort": true,
    "bAutoWidth": false,
    "bFilter": false,
    "bServersSide": true,
    "bJQueryUI": true,
    "oTableTools": {
        "aButtons": [],
        "sRowSelect": "single"
    },
    "sDom": 'T<"clear">lfrtip',

    // Server Parameters
    "fnServerParams": function ( aoData )
    {
        aoData.push( { "name": "invoiceDate", "value": "2012-10-10" } )
    },

    // Aajax Call
    "sAjaxSource": "/Invoice/GetDailyRateBillingRows",
    "bProcessing": false,
    "bRetrieve": true,
    "aoColumns": [
                    { "sName": "Detail" },
                    { "sName": "Qty" },
                    { "sName": "Price" },
                    { "sName": "RowTotal" }
                ]
} );
}

Action Method : needs to receive the invoice date and contract id

// Method to return InvoiceRows for DailyRate billing        
    public ActionResult GetDailyRateBillingRows(jQueryDataTableParamModel param)
    {
        // Hard coded. Need to receive parameters from Ajax post. (DataTables request.)
        DateTime invoiceDate = new DateTime(2012, 12, 31);
        int contractID = 1;


        int contractDayRate = db.Contracts.Where(c => c.Id == contractID).First().UnitRate;

        List<InvoiceRow> invoiceRows = new List<InvoiceRow>();

        List<DateTime> businessDaysOfMonth = new List<DateTime>();

        var firstDayOfMonth = new DateTime(invoiceDate.Year, invoiceDate.Month, 1);
        var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

        var holidays = db.Holidays.Where(h => h.DateOfHoliday >= firstDayOfMonth && h.DateOfHoliday <= lastDayOfMonth);

        // Get all the week days into businessDaysOfMonth
        for (DateTime date = firstDayOfMonth; date <= lastDayOfMonth; date = date.AddDays(1))
        {
            if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                businessDaysOfMonth.Add(date);
        }

        // Now remove the matching public holidays.
        foreach (var item in holidays)
        {
            businessDaysOfMonth.Remove(item.DateOfHoliday);
        }

        // .. and create list of invoiceRow items.
        foreach (var item in businessDaysOfMonth)
        {
            invoiceRows.Add(new InvoiceRow { InvoiceID = 0, ItemPrice = contractDayRate, RowDetail = GetDateString(item), RowQty = 1, RowTotal = contractDayRate });
        }

        var result = from c in invoiceRows
                     select new[] { c.RowDetail, c.RowQty.ToString(), c.ItemPrice.ToString(), c.RowTotal.ToString() };

        return Json(new { eEcho = param.sEcho, iTotalRecords = invoiceRows.Count(), iTotalDisplayRecords = invoiceRows.Count(), aaData = result }, JsonRequestBehavior.AllowGet);
    }

    private string GetDateString(DateTime date)
    {
        return date.ToString("dddd dd MMM yyyy");
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}

Upvotes: 2

Views: 2525

Answers (1)

Tohid
Tohid

Reputation: 6679

First of all, please notice fnServerParams is new since version 1.8.2, so please make sure you are running the latest release of dataTables.

You should be able to get it like this in your Controller method (GetDailyRateBillingRows):

var invoiceDate = HttpContext.Request["invoiceDate"];

Upvotes: 3

Related Questions