Jquery 向asp.net MVC控制器发送JSON数据时出现问题

Jquery 向asp.net MVC控制器发送JSON数据时出现问题,jquery,json,asp.net-mvc-3,Jquery,Json,Asp.net Mvc 3,在ASP.NET MVC 3中,我尝试将JSON数据发送到我的控制器时没有任何运气 我循环遍历一个列表,从元素生成JSON对象,然后将它们与我的查询参数一起发送: $.each(addedIngredients.find('li'), function () { ingredients[count] = { ID: $(this).attr('id').split('_')[1], Name: $(this).attr('id').split('_')[0

在ASP.NET MVC 3中,我尝试将JSON数据发送到我的控制器时没有任何运气

我循环遍历一个列表,从元素生成JSON对象,然后将它们与我的查询参数一起发送:

$.each(addedIngredients.find('li'), function () {
    ingredients[count] = {
        ID: $(this).attr('id').split('_')[1],
        Name: $(this).attr('id').split('_')[0]
    };
    count++;
});

request = $.ajax({
    url: '/Ingredients/SearchIngredients',
    data: {
        q: q,
        ingredients: ingredients
    },
    dataType: 'json',
    type: 'POST',
    success: function (result) {
             //Code omitted
    },
    error: function () {
             //Code omitted
    }
});
在控制器我有

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult SearchIngredients(string q, JSONIngredient[] ingredients)
    {
        //Initialise model
        List<JSONIngredient> model = new List<JSONIngredient>();

        //Add items to list
        ir.GetIngredients(q).ToList().ForEach(i => model.Add(new JSONIngredient(i)));

        //Return model as JSON object
        return this.Json(model);
    }
这是我编造的,因为我认为我的普通模型具有JSON中没有的附加属性,这是造成问题的原因,但我认为如果它真的起作用,它会与我的普通模型一起起作用

我想我发送数据的格式可能不正确。在firefox show中检查请求:

参数应用程序/x-www-form-urlencoded 成分[0][ID]4 成分[0][Name]水 q sug

来源

q=糖和配料%5B0%5D%5BName%5D=水和配料%5B0%5D%5BID%5D=4


任何帮助都将不胜感激。

在摆弄了一段时间后,我尝试了这个和那个,终于让它工作了。因为我怀疑数据格式不正确。而不是发送一个纯JSON对象

data: {
    q: q,
    ingredients: ingredients
}
我需要发送一个字符串化对象:

JSON.stringify({ q: q, ingredients: ingredients})

知道了就简单。

调用JSON.stringify处理数据。 有关示例,请参见此处:


首先尝试使用“q”,然后尝试使用“配料”

您在控制器中收到了什么?我总是为“q”获得正确的值,为“配料”获得空值。是的,刚才看到了,抱歉。但既然你花了时间回答,我就给你答案。
JSON.stringify({ q: q, ingredients: ingredients})