MVC3 Pass back List of Objects with Lists

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!

Answers


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.


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


Need Your Help

auto disposing of objects created in function calls

c# dispose using

I have a series of Read() overloads in a class.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.