2

I am trying to send an object array to my controller but having some difficulties.

It is sending the array and when delivered to the controller, the object count of the array also seems OK. But if you will look inside the objects all the attributes of the objects are null

How it can be possible?

JavaScript:

function callme(results) {
        for (var i = 0; i < results.length; i++) {
            var endRes = {
                Id: results[i].id,
                Icon: results[i].icon
            };
            jsonObj.push(endRes);
        }
        sendPackage(jsonObj);
}

function sendPackage(jsonObj) {
    $.ajax({
        type: "POST",
        url: '../../Home/RegisterList',
        data: { List: jsonObj },
        cache: false,
        dataType: "json",
        error: function (x, e, data) {
                alert(data);
        }
    });
}

Controller:

[HttpPost]
public JsonResult RegisterList(ICollection<DetailsModel> List)
{
    foreach (var i in List) ....... // other process will be here
    ............................... // other process will be here
    return Json(new { message = "OK" });
}

Model:

public class DetailsModel
{
    public string Id { get; set; }
    public string Icon { get; set; }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Görkem Öğüt
  • 1,761
  • 3
  • 18
  • 29
  • Are you sure that results[i].id isn't results[i].Id? Would make sense given your error: would add elements but with null values. – peterfoldi Feb 13 '12 at 17:12
  • I am sure and also object array seems OK with proper values before send. But somehow it is not reaching the controller. – Görkem Öğüt Feb 14 '12 at 10:20
  • 1
    A very similar question has a much simpler answer in another question http://stackoverflow.com/questions/7116099/send-array-to-mvc-controller-via-json – Appetere Sep 21 '12 at 17:14

2 Answers2

4

Ok I've solved the problem last night by using Newton's JSON.NET (you can get it from NuGet). I've stringified the array and recieved it as a string with the controller. Finally I used json.net to convert(deserialize) this string into a collection.

To stringify: use same code but change the data section of the json request with:

data: { List : JSON.stringify(jsonObj) }

Finally recieve it by:

using Newtonsoft.Json;
public JsonResult RegisterList(string List)
        {
            ICollection<DetailsModel> jsonModel = JsonConvert.DeserializeObject<ICollection<DetailsModel>>(List);
        }

And voila; you have a collection named jsonModel!

Görkem Öğüt
  • 1,761
  • 3
  • 18
  • 29
3

Unfortunately model binding of lists is not that nice and obvious in MVC. See: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Passing the list this way works:

data: {"List[0].Id":"1", "List[0].Icon":"test"}

peterfoldi
  • 7,451
  • 5
  • 21
  • 19
  • Now that's very strange, because I created a project with your code which reproduced your error (array is sized but all values are null) then changed this 1 line to the hard-coded test data I provided here and it works now (for me :) ). So if you have 5 minutes could you create a new project with the only functionality you showed here (1 button on the page to send the data) and let me know whether that also doesn't work for you? – peterfoldi Feb 14 '12 at 14:21
  • Ok I've solved the problem last night by using Newton's JSON.NET. I've stringified the array and recieved it as a string. Finally I used json.net to convert this string into a list. Thank you for spending your valuable time for me :) Really appreciate it – Görkem Öğüt Feb 15 '12 at 11:59