Title: Pass associative array of objects
Tags: c#;asp.net-mvc;arrays;associative
Question: I'm having a problem passing an associative array of objects to MVC Controller. I always get null.
This is what I have on client-side:
function Item() {
this.ItemId = GetRandomId('i');
this.Title = '';
this.Questions = [];
function Question() {
this.QuestionId = GetRandomId('q');
this.Description = '';
this.Values = [];
function QuestionValue() {
this.ValueId = GetRandomId('v');
this.Description = '';
this.IsCorrect = false;
I have an array 'items' and is filled like this:
items['i123'] = new Item();
On the server-side I mapped that:
public class EvItemJs
public int ItemId { get; set; }
public string Title { get; set; }
public EvQuestionJs[] Questions { get; set; }
public class EvQuestionJs
public int QuestionId { get; set; }
public string Description { get; set; }
public EvQuestionValueJs[] Values { get; set; }
public class EvQuestionValueJs
public int ValueId { get; set; }
public string Description { get; set; }
public string IsCorrect { get; set; }
My MVC Controller method is this:
[HttpPost]
public ActionResult Create(int userId, string userHash, EvItemJs[] items)
On the client-side I call the MVC Controller with this:
var data = { userId: global_data.userId, userHash: global_data.userHash, items: items };
$.ajax({
url: '/Evaluations/Create',
type: "POST",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
async: true,
cache: false,
success: function (msg) {
},
error: function (data, ajaxOptions, thrownError) {
The userId and userHash parameters have correct values, but the items is always null.
Anyone knows what is wrong? I tried with JSON.stringify and $.toJSON. I remember that sometime I did this with serialization but I cant find it.
Edit: this is what the browser sends to the server:
Comment: Can you paste the Json for the items that you are using? But I would also try to create an Object on Server Side and try to send a JSON from it and see how it creates that JSON. I will rather use that as a base and try to create using that particular JSON.

Comment: Can you try to send this json?
http://pastebin.com/0RbjqSBG

Comment: The only thing I can say now is you might want to try this http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object

Comment: @JinalShah there i posted the Json for the items.

Comment: @haim770 I tried with Dictionary but i only get 2 items with null values. What do you mean with register a custom Model-Binder?

Comment: @haim770 i tried the code, but it works if the items array is a key-value pair. My items array contains objects; you can map it into a dictionary, but i dont have a key-value in my array. I don't know how to map the "index" of the array (ie 'i123') to the key, and the object to the value of the dictionary :s

Comment: @JinalShah i tried it, but if i keep the MVC Controller method to EvItemJs[] it gets null again, but if i change it to object[], i get an array of the "definition of the array", like items[0][0] is a key-value object, with key = "userId" and value "global_data.userId", and no other data.

Comment: 1. Use `Dictionary` instead of `EvItemJs[] items`. 2. You'll have to register a custom Model-Binder for `IDictionary`.

Comment: See https://github.com/counsellorben/ASP.NET-MVC-JsonDictionaryBinding

Here is the accepted answer: i solved it converting the associative array to non-associative:
function NormalizeItemsArray() {
var result = new Array();
for (var i in items) {
i = items[i];
var item = new Item();
item.ItemId = i.ItemId;
item.Title = i.Title;
item.Questions = new Array();
for (var q in i.Questions) {
q = i.Questions[q];
var question = new Question();
question.QuestionId = q.QuestionId;
question.Description = q.Description;
question.Values = new Array();
for (var v in q.Values) {
v = q.Values[v];
var questionValue = new QuestionValue();
questionValue.ValueId = v.ValueId;
questionValue.Description = v.Description;
questionValue.IsCorrect = v.IsCorrect;
question.Values.push(questionValue);
}
item.Questions.push(question);
}
result.push(item);
return result;
Thanks to everybody!


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 9, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/09/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net-mvc><arrays><associative>', 'Title': 'Pass associative array of objects', 'ViewCount': 1139, 'id': 21660375, 'user_AboutMe': "<p>Currently I'm working as Software Architect / Project Manager at GMigliarino Consultores, developing mostly web applications based on Microsoft technologies like C#, ASP.NET MVC, SQL Server making focus on Quality and User Experience.\nOn the other side, I work as Freelance Developer and IT Consultant.</p>\n\n<p>On my free-time, I do Crossfit and Photography.</p>\n", 'user_AccountId': 3325553.0, 'user_DisplayName': 'tincho87', 'user_Reputation': 309.0, 'user_Views': 27.0, 'user_id': 2795721.0}}