をASP.NETするJSONデータを送信します。トラブル私は私のコントローラにJSONデータを送信しようとしているすべての運を持っていないですMVCコントローラにASP.NET MVC 3では
Iループのリストを通って、要素からJSONオブジェクトを生成し、その後、私のクエリパラメータと一緒にそれらをオフに送信:私はJSONingredientが
ある[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);
}
を持つコントローラで
$.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
}
});
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
ソース
Q = SUG &成分の%5B0%5Dとの%の5BNameの%5D =水&成分%5B0%5Dとの%5BID%5D = 4
任意の助けもいただければ幸いです。しばらくの間、これで周りいじると、これを試した後、私は最終的にそれが動作するようになったこと
を試し、その後、単に「Q」で最初に試してみてください? – jgauffin
私はいつも「成分」のための「Q」とナルの正しい値を取得します。良い –