P.クライアント上でJSON.stringify()を使用でき、サーバー上に JSONパーサーが使用できるようですが、それは私のハックです。より良い 方法はありますか?
JSON.stringifyで十分です。JSONパーサーはサーバー上には必要ありません。これは、基本フレームワークによって既に処理されています。
例:あなたのAJAXリクエストの2つの重要なパラメータは以下の通りです
[HttpPost]
public ActionResult SomeAction(MyModel model)
{
... use your model here directly without any JSON parsing
}
:
contentType: 'application/json',
data: JSON.stringify(model)
との成功し、次のアクションに送信されます
var model = { items: [] };
// now add some elements to the items array
model.items.push({ id: 1 });
model.items.push({ id: 2 });
model.items.push({ id: 3 });
...
$.ajax({
url: 'your controller action endpoint comes here',
method: 'post',
contentType: 'application/json',
data: JSON.stringify(model),
success: function(result) {
// do something with the results here
}
});
もちろん、model
javascript変数にはサーバーのサイドビューmoが反映されている必要がありますdel構造体(items
の要素の配列プロパティはid
プロパティを持つ必要があります)。
お客様のクライアント側のコードを示します。 –