传递对象关联数组

3

我遇到了一个问题,无法将一个对象数组传递给MVC控制器。我总是得到null

这是我在客户端的代码:

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;
}

我有一个数组'items',它被填充成这样:

items['i123'] = new Item();

在服务器端,我将其映射为:

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; }
}

我的MVC控制器方法如下:

[HttpPost]
public ActionResult Create(int userId, string userHash, EvItemJs[] items)

在客户端,我使用以下代码调用MVC控制器:
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) {

    }
});

userId和userHash参数的值是正确的,但是items始终为null。

有人知道出了什么问题吗?我尝试过使用JSON.stringify$.toJSON。我记得有时候我会用序列化解决这个问题,但是我找不到它在哪里。

编辑:这是浏览器发送给服务器的内容:

what the browser sends to the server


你能粘贴一下你正在使用的项目的Json吗?但我也会尝试在服务器端创建一个对象,并尝试从中发送JSON,看看它如何创建该JSON。我更愿意使用它作为基础,并尝试使用特定的JSON来创建。 - Jinal Shah
  1. ن½؟用Dictionary<string, EvItemJs>و›؟ن»£EvItemJs[] itemsم€‚
  2. و‚¨éœ€è¦پن¸؛IDictionary<string, EvItemJs>و³¨ه†Œن¸€ن¸ھè‡ھه®ڑن¹‰çڑ„و¨،ه‍‹ç»‘ه®ڑه™¨م€‚
- haim770
@JinalShah,我已经发布了项目的Json。 - tincho87
请参见 https://github.com/counsellorben/ASP.NET-MVC-JsonDictionaryBinding。 - haim770
你能试着发送这个JSON吗? http://pastebin.com/0RbjqSBG - Jinal Shah
显示剩余4条评论
1个回答

1

我通过将关联数组转换为非关联数组来解决了它:

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;
}

感谢大家!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接