如何在MVC3中访问JQuery DataTables插件aoData值?

时间:2011-12-14 13:55:43

标签: jquery asp.net-mvc-3 datatables

我正在使用MVC 3.如何在控制器上的aoData对象中访问fnServerParams发送的数据?感谢

更新:这是我试图使用的jquery ......

 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" }
                ]
} );
}

操作方法:需要接收发票日期和合同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);
    }
}

1 个答案:

答案 0 :(得分:3)

首先,请注意fnServerParams是自1.8.2版以来的新版本,因此请确保您运行的是最新版本的dataTables。

你应该能够在你的Controller方法(GetDailyRateBillingRows)中得到这样的结果:

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