2011-12-14 46 views
2

我正在使用MVC 3.如何訪問由控制器上的aoData對象中的fnServerParams發送的數據?由於如何訪問MVC3中的JQuery DataTables插件aoData值?

UPDATE:這是我想使用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" } 
       ] 
}); 
} 

操作方法:需要接收發票日期和合同編號

// 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); 
    } 
} 
+0

http://stackoverflow.com/questions/4201849/whats-the-correct-ajax-的可能的複製使用jquery-datatable -with-asp-net-mvc/4221720#4221720 – 2011-12-14 14:00:11

回答

3

首先,請通知fnServerParams是自1.8.2版本以來的新增功能,因此請確保您正在運行最新版本的dataTables。

你應該能夠得到它像這樣在你的控制方法(GetDailyRateBillingRows):

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