1

I need to pass an array of objects, which contain arrays, from javascript to a MVC actionresult method. Basically I have a KeyValuePair whose key is an array of strings, and I need to pass back a list of these KeyValuePairs. In the code, I have a list of keys I've changed, and a JS object that holds key->value pairs. My javascript object that I try to pass back should be the same format as my class. I tried JSONing it as well, but no luck.

Thanks.

public class ChangedItem
{
    public IEnumerable<string> Key { get; set; }
    public int Value { get; set; }

    public ChangedItem()
    {
    }

    public ChangedItem(IEnumerable<string> key, int value)
    {
        Key = key;
        Value = value;
    }
}

[HttpPost]
public ActionResult UpdateResults(IEnumerable<ChangedItem> changedItems)
{
    return RedirectToAction("Index");
}

function getChangedItems() {
    var ChangedItems = new Array();
    for (var i = 0; i < _ChangedItemKeys.length; i++) {
        var ChangedItem = {};
        ChangedItem.Key = _ChangedItemKeys[i];
        ChangedItem.Value = _ChangedItems[_ChangedItemKeys[i]];
        ChangedItems[i] = ChangedItem;
    }
    return ChangedItems;
}

function submitNewVals() {
    $.ajax({
        url: '/Home/UpdateResults',
        type: 'POST',
        dataType: 'json',
        data:
        {
            changedItems: getChangedItems()
        }
    });
}

My request data is detailed below, and I get 6 items in my action result, its just that inside each of those items the key is null and value is 0:

changedItems[0][Key][]:BoxeR
changedItems[0][Key][]:Proleague
changedItems[0][Key][]:Total
changedItems[0][Key][]:2005
changedItems[0][Key][]:Quarter 2
changedItems[0][Key][]:Actual
changedItems[0][Value]:123
changedItems[1][Key][]:BoxeR
changedItems[1][Key][]:Proleague
changedItems[1][Key][]:Team
changedItems[1][Key][]:2005
changedItems[1][Key][]:Quarter 1
changedItems[1][Key][]:Actual
changedItems[1][Value]:123
changedItems[2][Key][]:BoxeR
changedItems[2][Key][]:Proleague
changedItems[2][Key][]:Team
changedItems[2][Key][]:2005
changedItems[2][Key][]:Quarter 1
changedItems[2][Key][]:Estimate
changedItems[2][Value]:123
changedItems[3][Key][]:BoxeR
changedItems[3][Key][]:Proleague
changedItems[3][Key][]:Team
changedItems[3][Key][]:2005
changedItems[3][Key][]:Quarter 2
changedItems[3][Key][]:Actual
changedItems[3][Value]:123
changedItems[4][Key][]:BoxeR
changedItems[4][Key][]:Proleague
changedItems[4][Key][]:Team
changedItems[4][Key][]:2005
changedItems[4][Key][]:Quarter 2
changedItems[4][Key][]:Estimate
changedItems[4][Value]:123

I tried playing around in fiddler and changing the request data format (using .Key[x], .Value), but nothing worked.

Thanks!

2 Answers 2

1

The format should be like this:

changedItems[0].Key[0]=BoxeR
changedItems[0].Key[1]=Proleague
changedItems[0].Key[2]=Total
changedItems[0].Key[3]=2005
changedItems[0].Key[4]=Quarter 2
changedItems[0].Key[5]=Actual
changedItems[0].Value=123
...

See ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries for details.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks. Do you know how to get .ajax() to use this format without "faking" it by creating a bunch of inputs + a form?
If you do a GET request, you can add those to the query arguments and that works fine. Otherwise you can explicitly set the data to be sent, and you just have to make sure to build the appropriate key names for the values being sent.
1

sorry but your return data type is json and you pass array please change your code return type JsonResult

return Json(ChangedItems, JsonRequestBehavior.AllowGet);

see this link

1 Comment

While this is correct and a good hint, it doesn't answer the question at hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.