1
Jquery $ .ajaxポストから複合型を受け取るレストサービスを作成しようとしていますが、コントローラ内の複雑なオブジェクトを水和させることができません。JqueryからAsp.NetへのMVCを使用した複合型の転記
次は私のコードの一部です:
コントローラー:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChargeUser(TransactionInfo transactionInfo, CardInfo cardInfo) {
/// both transactionInfo and cardInfo are un-populated.
}
のDTO:
"{"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"}}"
:私の投稿しています
[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は、次のようになります
jquを使用するこのようなERYコール:デバッグ時
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どちらも人口だけデフォルト刺されの値(ヌル)と数字(0)を持っています。
jsonの書式設定で何か問題が発生していると想定していますが、わかりません。どんな洞察力も大変高く評価されます。
お時間を頂きまして誠にありがとうございます。
ハル
だから、フラットな構造が好きですか?先端に感謝します。私は午前中にそれをチェックアウトし、私の結果を掲載します。 – Hal
うわー、これは本当に残念です。私は彼らが2.0リリースのためにこれを変更する予定かどうか疑問に思います... var obj1 {id:1、obj2:{prop:val}}; var objよりもはるかにクリーンです1 {id:1、 "obj2.prop":val};私の考えでは。タイプをJavaScriptに動的に公開すると、操作が簡単になります。 – Langdon