2

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);
    }
}
6
  • Possible duplicate of stackoverflow.com/questions/4201849/… Commented Dec 14, 2011 at 14:00
  • Question above is more of a design pattern question. My question is asking how one accesses aoData on the controller Commented Dec 14, 2011 at 14:10
  • You cannot access aoData on the controller, you can only pass it from your jQuery code to the controller, for instance by means of an Ajax call. Commented Dec 14, 2011 at 14:12
  • 1
    I am trying to pass parameters to the controller that will be used to build data for the response. As an example I am using the following code:// Server Parameters "fnServerParams": function ( aoData ) { aoData.push( { "name": "invoiceDate", "value": "2012-10-10" } ) }, .... Is this approach wrong? If so, how can I send additional data to the server? Commented Dec 14, 2011 at 14:29
  • 1
    OK - I found it in the Request.QueryString because its a GET! Doh, shoulda looked there first. Commented Dec 14, 2011 at 14:55

1 Answer 1

3

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"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.