将 JSON 数据发送到 ASP.NET MVC 控制器时遇到问题

发布于 2025-01-08 02:41:31 字数 1745 浏览 0 评论 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);
    }

Where JSONingredient is

public class JSONIngredient
{
    public int ID { get; set; }

    public string Name { get; set; }

    public JSONIngredient()
    {
    }

    public JSONIngredient(Ingredient Ingredient)
    {
        this.ID = Ingredient.ID;
        this.Name = Ingredient.Name;
    }
}

这是我编造的,因为我认为我的正常模型具有不在其中的附加属性JSON 导致了这个问题,但我认为如果它确实有效,它会与我的正常模型一起工作......

我想也许我发送数据的格式不正确。检查 Firefox 中的请求显示:

Parametersapplication/x-www-form-urlencoded 成分[0][ID]4 成分[0][名称] 水 q sug

Source

q=sug&ingredients%5B0%5D%5BName%5D=Water&ingredients%5B0%5D%5BID%5D=4

任何帮助将不胜感激。

In ASP.NET MVC 3 I am not having any luck trying to send JSON data to my controller.

I loop through a list and generate JSON objects from the elements and then send them off along with my query parameter:

$.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
    }
});

At the controller I have

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

Where JSONingredient is

public class JSONIngredient
{
    public int ID { get; set; }

    public string Name { get; set; }

    public JSONIngredient()
    {
    }

    public JSONIngredient(Ingredient Ingredient)
    {
        this.ID = Ingredient.ID;
        this.Name = Ingredient.Name;
    }
}

Which I made up because I thought my normal model which has additional properties which are not in the JSON was causing the problem, but I would have thought that if it did work it would have worked with my normal model...

I'm thinking that maybe the format in which I am sending the data is not correct. Examining the request in firefox show:

Parametersapplication/x-www-form-urlencoded
ingredients[0][ID] 4
ingredients[0][Name] Water
q sug

Source

q=sug&ingredients%5B0%5D%5BName%5D=Water&ingredients%5B0%5D%5BID%5D=4

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2025-01-15 02:41:31

围绕您的数据调用 JSON.stringify。
请参阅此处的示例:
通过 JSON 将对象数组发布到 ASP。 Net MVC3

首先尝试仅使用“q”,然后尝试仅使用“成分”

Call JSON.stringify around your data.
See here for an example:
Post an Array of Objects via JSON to ASP.Net MVC3

Try first with just 'q', then try as just 'ingredients'

落日海湾 2025-01-15 02:41:31

经过一段时间的摆弄和尝试后,我终于让它发挥作用了。因为我怀疑数据格式不正确。 发送一个字符串化对象,而不是发送一个纯 JSON

data: {
    q: q,
    ingredients: ingredients
}

我需要

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

对象:如果您知道的话,很简单。

After fiddling around with this for a while and trying this and that I finally got it to work. As I suspected the data format was incorrect. Instead of sending a pure JSON object

data: {
    q: q,
    ingredients: ingredients
}

I needed to send a stringified object:

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

Simple when you know it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文