c# - 传递对象的关联数组

标签 c# asp.net-mvc arrays associative

我在将对象的关联数组传递给 MVC Controller 时遇到问题。我总是得到 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 Controller 方法是这样的:

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

在客户端,我用这个调用 MVC Controller :

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 参数具有正确的值,但项目始终为 null。

有谁知道是怎么回事吗?我尝试使用 JSON.stringify$.toJSON。我记得有一次我用序列化做了这个,但我找不到它。

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

what the browser sends to the server

最佳答案

我解决了将关联数组转换为非关联数组的问题:

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

谢谢大家!

关于c# - 传递对象的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21660375/

相关文章:

c# - Type.GetProperties 不返回任何属性

c# - 我的单元测试有效, Controller 中的相同代码不起作用

c++ - 如何遍历不同类型类的数组

javascript - 路由未按预期工作

c# - 优化 C# 代码片段

c# - 如何在 C# 中使用代理访问 Web 服务

asp.net-mvc - SignalR Hub ResolutionFailedException 与 Unity

javascript - 是否有支持在服务器和客户端上共享 View 的 .Net MVC 解决方案?

c - 为什么数组索引和地址与分配给数组的字符串具有相同的值?

javascript - 在对象数组中循环 for