views:

377

answers:

1

I am trying to make a rest service that receives complex types from a Jquery $.ajax post but I cannot seem to convince mvc to hydrate my complex objects in the controller.

The following is some of my code:

Controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChargeUser(TransactionInfo transactionInfo, CardInfo cardInfo) {
/// both transactionInfo and cardInfo are un-populated.   
}

DTOs:

[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; }
}

Sample JSON that I am POSTing, looks like this:

"{"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"}}"

Using a jquery call like this:

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
    });    
}

I hit the break point when debugging, but neither transactionInfo nor cardInfo are populated and have only default values for stings (null) and numerics (0).

I assume that I am doing something wrong with my json formatting, but simply cannot figure it out. Any insight would be greatly appreciated.

Best regards and thank you for your time.

Hal

+1  A: 

I think the way the model binder is expecting the query string to look would be more like:

TransactionInfo.FirstName=Hal&TransactionInfo.LastName=Lesesne&...

If your object is like:

{ "TransactionInfo.FirstName" : "Hal", "TransactionInfo.LastName", "Lesesne", ... }

Then I think it will correctly serialize it into the query string that MVC expects.

tvanfosson
So it prefers a flat structure? Thanks for the tip. I'll check it out first thing in the AM and post my results.
Hal
Wow, this is really unfortunate. I wonder if they plan on changing this for the 2.0 release... var obj1 { id : 1, obj2 : { prop : val } }; is so much cleaner than var obj 1 { id : 1, "obj2.prop" : val }; in my opinion. And easier to work with if you dynamically expose your types to JavaScript.
Langdon