我试图创建一个从Jquery$.ajax帖子接收复杂类型的rest服务,但我似乎无法说服mvc在控制器中水合我的复杂对象。
以下是我的一些代码:
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChargeUser(TransactionInfo transactionInfo, CardInfo cardInfo) {
/// both transactionInfo and cardInfo are un-populated.
}
[Serializable] public class CardInfo : ICardInfo {
public string CCNumber { get; set; }
public int ExpirationMonth { get; set; }
public int ExpirationYear { get; set; }
public string CardVerificationValue { get; set; }
}
[Serializable]
public class TransactionInfo : ITransactionInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
public string Currency { get; set; }
public decimal Amount { get; set; }
}
我正在发布的JSON示例如下:
"{"transactionInfo":{"FirstName":"Hal","LastName":"Lesesne","Address1":"504 Anytown Drive","Address2":"Sample Address 2","City":"Boone","Region":"NC","Country":"US","PostalCode":"28607","Currency":"USD","Amount":"1.5"},"cardInfo":{"CCNumber":"4222 2222 2222 2222","ExpirationMonth":"1","ExpirationYear":"2009","CardVerificationValue":"333"}}"
function jQueryPost(data, action, onSuccess, onFailure) {
$.ajax({
url: action,
type: 'POST',
data: data,
dataType: 'json',
contentType: "application/json; charset=utf-8",
error: onFailure,
success: onSuccess
});
}
我在调试时遇到了断点,但transactionInfo和cardInfo都没有填充,只有默认值(null)和数字(0)。
我假设我的json格式有问题,但根本无法弄清楚。任何见解都将不胜感激。
哈尔