This was my first attempt at JSON MVC model binding and things aren't going according to plan.... massive sigh! In every attempt when I break in the controller action the strongly typed model/class is either empty or null.
What I do:
Client side I'm generating an array to store payments
var PAYMENTS = new Array();
Payment values are entered by a user and added into the payments array. Some of the fields will only be populated server side once data is posted, so I assign temp values.(reference, userID)
PAYMENTS.push({ reference: "0",
paymentDate: date,
paymentMethodID: paymentMethod,
paymentAmount: amount,
bankNameID: bankID,
bankBranchCode: branch,
drawerName: drawer,
chequeNo: chequeNo,
userID: "00000000-0000-0000-0000-000000000000",
statementNo: statementNo,
additionalReference: additionalRef
});
After adding 2 payments, this is the firebug result for PAYMENTS
0 Object { reference="0", paymentDate="31-01-2012", paymentMethodID="7", more...}
1 Object { reference="0", paymentDate="31-01-2012", paymentMethodID="0", more...}
User selects to process the payments which calls processPayment()
Practices are a 2nd parameter that should be passed, but just trying to get payments working first.
function processPayment() {
var paymentsJSON = $.toJSON(PAYMENTS);
// var practicesJSON = $.toJSON(selectedPractices);
$.ajax({
url: BASE_APP_URL + "Payment/processPayment",
type: 'POST',
data: "{ param : '" + paymentsJSON + "'}" ,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
alert(data);
}
});
}
viewModel Class. I've simplefied the viewModel types in an attempt to get something into the controller.
public class paymentsViewModelJSONPayment
{
public string referece { get; set; }
public string paymentDate { get; set; }
public string paymentMethodID { get; set; }
public decimal paymentAmount { get; set; }
public string bankNameID { get; set; }
public string bankBranchCode { get; set; }
public string drawerName { get; set; }
public string chequeNo { get; set; }
public string userID { get; set; }
public string statementNo { get; set; }
public string additionalReference { get; set; }
}
finally controller action
[HttpPost]
public ActionResult processPayment(List<paymentsViewModelJSONPayment> param)
{
return Json(param);
When debugging:
Post values from Firebug:
JSON
param "[{"reference":"0","paymentDate":"31-01-2012","paymentMethodID":"7","paymentAmount":500,"bankNameID":"8","bankBranchCode":"","drawerName":"","chequeNo":"","userID":"00000000-0000-0000-0000-000000000000","statementNo":"","additionalReference":""},{"reference":"0","paymentDate":"31-01-2012","paymentMethodID":"0","paymentAmount":500,"bankNameID":"10","bankBranchCode":"0026","drawerName":"drawer","chequeNo":"00231021","userID":"00000000-0000-0000-0000-000000000000","statementNo":"","additionalReference":""}]"
Breakpoint in controller. The action gets called but no binding.
Not allowed to post images yet, but when breaking controller List is empty.
From watchlist:
- param Count = 0 System.Collections.Generic.List
- Raw View Capacity 0 int Count 0 int
- Static members
- Non-Public members
I'm pretty much out of ideas, tried various configurations and kinda lost count of everything I've tried.
Will greatly appreciate any help.
Thanks a stack.
