这是我第一次尝试JSON MVC模型绑定,事情不按计划进行......大声叹息!在我打破控制器动作的每次尝试中,强类型模型/类都是空的或空的。
我的所作所为:
客户端我正在生成一个存储付款的数组
var PAYMENTS = new Array();
付款值由用户输入并添加到付款数组中。一旦发布数据,某些字段将仅填充服务器端,因此我分配临时值。(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
});
添加2笔付款后,这是PAYMENTS的萤火虫结果
0 Object { reference="0", paymentDate="31-01-2012", paymentMethodID="7", more...}
1 Object { reference="0", paymentDate="31-01-2012", paymentMethodID="0", more...}
用户选择处理调用processPayment()
的付款实践是应该传递的第二个参数,但只是尝试先付款。
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类。我已经简化了viewModel类型,试图让某些东西进入控制器。
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; }
}
最后控制器动作
[HttpPost]
public ActionResult processPayment(List<paymentsViewModelJSONPayment> param)
{
return Json(param);
调试时:
从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":""}]"
控制器中的断点。该动作被调用但没有绑定。
不允许发布图像,但是当打破控制器列表为空时。
来自关注列表:
- param Count = 0 System.Collections.Generic.List
- 原始视图容量0 int Count 0 int
- 静态成员
- 非公开会员
我几乎没有想法,尝试了各种配置,有点失去了我尝试过的所有内容。
非常感谢任何帮助。
感谢筹码。
答案 0 :(得分:3)
尝试为500.0
指定paymentAmount
。我喜欢使用Fiddler2来测试这样的事情:http://www.fiddler2.com/fiddler2/。
此链接揭示了绑定十进制值:http://digitalbush.com/2011/04/24/asp-net-mvc3-json-decimal-binding-woes/
编辑:我使用您的类设置了一个测试项目,并且能够使用Fiddler将您的JSON(但是调整paymentAmount
)发送到控制器而没有任何问题。
请求显示在顶部窗格中,底部显示响应(由Fiddler格式化为JSON)。
您班级中的referece
属性拼写错误,因此会以null
的形式返回。
答案 1 :(得分:-1)
我可以将值绑定到'param'的唯一方法是将其定义为字符串。然后我使用JavascriptSerializer将其反序列化为我想要的对象。
[HttpPost]
public JsonResult processPayment(string param)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
try
{
List<paymentsViewModelJSONPayment> payments = serializer.Deserialize<List<paymentsViewModelJSONPayment>>(param);
断点处的参数值
"[{\"reference\":\"0\",\"paymentDate\":\"01-02-2012\",\"paymentMethodID\":\"7\",\"paymentAmount\":500,\"bankNameID\":\"8\",\"bankBranchCode\":\"\",\"drawerName\":\"\",\"chequeNo\":\"\",\"userID\":\"00000000-0000-0000-0000-000000000000\",\"statementNo\":\"\",\"additionalReference\":\"\"}]"
VS watchlsit中的付款值
payments Count = 1
System.Collections.Generic.List<PCNS.viewModels.paymentsViewModelJSONPayment>
[0] {PCNS.viewModels.paymentsViewModelJSONPayment} PCNS.viewModels.paymentsViewModelJSONPayment
additionalReference "" string
bankBranchCode "" string
bankNameID "8" string
chequeNo "" string
drawerName "" string
paymentAmount 500 decimal
paymentDate "01-02-2012" string
paymentMethodID "7" string
referece null string
statementNo "" string
userID "00000000-0000-0000-0000-000000000000" string
看起来有点像黑客,但它正在发挥作用。