将 JSON 数据发送到 ASP.NET MVC 控制器时遇到问题
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
围绕您的数据调用 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'
经过一段时间的摆弄和尝试后,我终于让它发挥作用了。因为我怀疑数据格式不正确。 发送一个字符串化对象,而不是发送一个纯 JSON
我需要
对象:如果您知道的话,很简单。
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
I needed to send a stringified object:
Simple when you know it.