Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding an array of objects in MVC

I am having a problem binding an array of key value pair objects in MVC5. This is the JSON:

{
    "expiration": "2013-12-03T04:30:41.206Z",
    "conditions": [
        {
            "acl": "private"
        },
        {
            "bucket": "ryvus.upload.test"
        },
        {
            "Content-Type": "application/vnd.ms-excel"
        },
        {
            "success_action_status": "200"
        },
        {
            "key": "fc42ae8a-1f6e-4955-a669-8272ad650cb9.csv"
        },
        {
            "x-amz-meta-qqfilename": "simpleupload.csv"
        }
    ]
}

If I attempt to bind conditions to a Dictionary<string, string> like this:

// View Model
public class ResponseVM
{
    public DateTime Expiration { get; set; }
    public Dictionary<string, string> Conditions { get; set; }
}
// Controller Action
public ActionResult MyHandler(ResponseVM s3Response)
{
   //do something
   return JSON(null);
}

I get a 6 entries with keys of "0","1","2","3","4","5" and null values. I seem so close but I've tried a bunch of different types without success, the Dictionary<string, string> was the best I could get.

Am I missing something completely obvious here?

like image 896
dohmoose Avatar asked Mar 26 '26 04:03

dohmoose


1 Answers

Since the JSON scheme can't be changed, the easiest way is the change the class to match the JSON.

public class ResponseVM
{
    public DateTime Expiration { get; set; }
    public List<Dictionary<string, string>> Conditions { get; set; }
}

Condition is a list of maps.

By the way, the purpose of view models (VM) is for abstracting the view. It's not for input.

like image 134
LostInComputer Avatar answered Mar 28 '26 17:03

LostInComputer