如何访问JQuery的数据表中MVC3插件aoData值? [英] How do I access the JQuery DataTables plugin aoData values in MVC3?

查看:97
本文介绍了如何访问JQuery的数据表中MVC3插件aoData值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

更新:这是我想使用jQuery的...

 函数GenerateRows()
 {
 VAR serverParams = {invoiceDate:,contractID:} serverParams.invoiceDate = $(#InvoiceDate).VAL();
 serverParams.contractID = $(#ContractID).VAL(); $('#invoiceRows').dataTable({    //表格样式
    bPaginate:假的,
    bLengthChange:假的,
    bSort:真实,
    bAutoWidth:假的,
    bFilter:假的,
    bServersSide:真实,
    bJQueryUI:真实,
    oTableTools:{
        aButtons:[],
        sRowSelect:单
    },
    sDom:T<清> lfrtip',    //服务器参数
    fnServerParams:功能(aoData)
    {
        aoData.push({名:invoiceDate,值:2012年10月10日})
    },    // Aajax呼叫
    sAjaxSource:/发票/ GetDailyRateBillingRows
    bProcessing:假的,
    bRetrieve:真实,
    aoColumns:[
                    {SNAME:详细信息},
                    {SNAME:数量},
                    {SNAME:价格},
                    {SNAME:RowTotal}
                ]
});
}

操作方法:需要接收发票日期和合同编号

  //方法返回InvoiceRows为DailyRate计费
    公众的ActionResult GetDailyRateBillingRows(jQueryDataTableParamModel参数)
    {
        //硬codeD。需要从阿贾克斯后接收参数。 (数据表的要求。)
        日期时间invoiceDate =新的日期时间(2012,12,31);
        INT contractID = 1;
        INT contractDayRate = db.Contracts.Where(C => c.Id == contractID)。首先()UnitRate。        清单< InvoiceRow> invoiceRows =新的List< InvoiceRow>();        清单<&日期时间GT; businessDaysOfMonth =新的List<&日期时间GT;();        VAR firstDayOfMonth =新日期时间(invoiceDate.Year,invoiceDate.Month,1);
        变种lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);        VAR节假日= db.Holidays.Where(H => h.DateOfHoliday> = firstDayOfMonth和放大器;&安培; h.DateOfHoliday< = lastDayOfMonth);        //获取所有的工作日到businessDaysOfMonth
        为(日期时间日期= firstDayOfMonth;日期和LT = lastDayOfMonth;日期= date.AddDays(1))
        {
            如果(date.DayOfWeek = DayOfWeek.Saturday和放大器;!&安培;!date.DayOfWeek = DayOfWeek.Sunday)
                businessDaysOfMonth.Add(日期);
        }        //现在删除匹配的公共假期。
        的foreach(节假日VAR项)
        {
            businessDaysOfMonth.Remove(item.DateOfHoliday);
        }        // ..创造的invoiceRow项目清单。
        的foreach(在businessDaysOfMonth VAR项)
        {
            invoiceRows.Add(新InvoiceRow {InvoiceID = 0,ITEMPRICE = contractDayRate,RowDetail = GetDateString(项目),RowQty = 1,RowTotal = contractDayRate});
        }        VAR的结果=从invoiceRowsÇ
                     选择新的[] {c.RowDetail,c.RowQty.ToString(),c.ItemPrice.ToString(),c.RowTotal.ToString()};        返回JSON(新{eEcho = param.sEcho,iTotalRecords = invoiceRows.Count(),iTotalDisplayRecords = invoiceRows.Count(),aaData =结果},JsonRequestBehavior.AllowGet);
    }    私人字符串GetDateString(DateTime的日期)
    {
        返回与Date.toString(DDDD DD MMM YYYY);
    }    保护覆盖无效的Dispose(BOOL处置)
    {
        db.Dispose();
        base.Dispose(处置);
    }
}


解决方案

首先,请注意 fnServerParams 是自1.8.2版本新,所以请确保您正在运行数据表的最新版本。

您应该能够得到像这样在你的控制方法( GetDailyRateBillingRows

  VAR invoiceDate = HttpContext.Request [invoiceDate];

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

解决方案

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

这篇关于如何访问JQuery的数据表中MVC3插件aoData值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆