MVC 3 : How do I access fnServerParams on the server?
MVC 3 : How do I access fnServerParams on the server?
grs
Posts: 2Questions: 0Answers: 0
I am using MVC3 and I am trying to pass extra parameters to the server but I dont know how to access the parameter values in the controller.
Ajax function:
[code]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( serverParams )
},
// Aajax Call
"sAjaxSource": "/Invoice/GetDailyRateBillingRows",
"bProcessing": false,
"bRetrieve": true,
"aoColumns": [
{ "sName": "Detail" },
{ "sName": "Qty" },
{ "sName": "Price" },
{ "sName": "RowTotal" }
]
} );
}[/code]
MVC 3 Controller Method:
[code] // 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 invoiceRows = new List();
List businessDaysOfMonth = new List();
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);
}[/code]
Ajax function:
[code]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( serverParams )
},
// Aajax Call
"sAjaxSource": "/Invoice/GetDailyRateBillingRows",
"bProcessing": false,
"bRetrieve": true,
"aoColumns": [
{ "sName": "Detail" },
{ "sName": "Qty" },
{ "sName": "Price" },
{ "sName": "RowTotal" }
]
} );
}[/code]
MVC 3 Controller Method:
[code] // 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 invoiceRows = new List();
List businessDaysOfMonth = new List();
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);
}[/code]
This discussion has been closed.
Replies
[code]
aoData.push(name:"invoiceDate", value:$( "#InvoiceDate" ).val());
aoData.push(name:"contractID", value:$( "#ContractID" ).val(););
[/code]
In controller method, "invoiceDate" and "contractID" are accessible via request.querystring
I hope this helps