2

私の人生では、このデータをバインドすることはできません。これが私のJavaScriptです:

var params = { 'InvItemIDs': ["188475", "188490"]};
$.post("api/Orders/OrderFromInventory?" + $.param(params))

そしてコントローラーアクション:

public HttpResponseMessage OrderFromInventory(IList<int> InvItemIDs)
{
    return new HttpResponseMessage();
}

送信するようにクエリ文字列を作成しました。

?InvItemIDs=188475&InvItemIDs=188490

としても

?InvItemIDs[]=188475&InvItemIDs[]=188490

そしてさえ

?InvItemIDs[0]=188475&InvItemIDs[1]=188490

そして、それらのどれも拘束力がありません。InvItemIDsは常にnull。私は何を間違っていますか?

編集:

つまり、これはすべて、MVC4 の新しい Web API コントローラー コードのバグ (または何か) であることが判明しました。まったく同じコードを標準コントローラーに移動するとすぐに、動作し始めました。

Web API がこのバインディングを破る理由について誰かが洞察を持っているかどうか、私はまだ興味があります。

4

2 に答える 2

2

The reason it doesn't work is because you are using [HttpPost] where it will be expecting the data to be posted in "post body" instead of URL.

You can either

1, remove httpPost
2. put the list in the post content

You would need to set the "traditional: true" for the array to work. Here is a sample code that I've tested on my local project, give that a try

var InvItemIDs = ["188475", "188490"];
$.ajax({ type: "POST",
    url: "Home/TestIndex",
    datatype: "json",
    traditional: true,
    data:
    {
        'InvItemIDs': InvItemIDs
    }              
});
于 2012-12-03T23:27:36.490 に答える
1

このHaacked ブログ投稿が役立つ場合があります。具体的には、彼の最初の例を見ると、 に変更IListするとどうなりICollectionますか?

このようなものは「動作するはず」です

[HttpGet]
public HttpResponseMessage OrderFromInventory(IList<int> InvItemIDs)
{
    return new HttpResponseMessage();
}

クエリ文字列で

?InvItemIDs=188475&InvItemIDs=188490
于 2012-12-03T23:56:21.053 に答える