1

jQuery Ajax post を使用して要素の配列をコントローラー アクションに投稿するにはどうすればよいですか。

これは私がしようとしている方法ですが、私のコントローラーはnull配列を受け取ります。

function dipatchAllocations() {
            // unSelected is an array of items of type int.
            if (unSelected.length == 0) {
                alert("Please select a line item to be dispatched.");
                return;
            }
            debugger;
            $.ajax({
                type: 'POST',
                url: '/Batch/SetToDispatch',
                data: '{ "allocationId[]" : "[' + unSelected + ']","batchId" : "' + @Model.Id + '" }',
                contentType: "application/json; charset=utf-8",
                traditional: true,
                success: updateView,
                error: errorInSubscribing
            });
        };

そして、これは私のコントローラーです

[HttpPost]
public ActionResult SetToDispatch(long[] allocationId,long batchId)
{   
  // Perform some action here
   return View("_ReadyToDispatchItems",model);
}

誰かが私に欠けているものを教えてくれますか?

ありがとう

4

3 に答える 3

1

これを試して:

var data = { allocationId : unSelected, batchId : @Model.Id };
$.ajax({
                type: 'POST',
                url: '/Batch/SetToDispatch',
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                traditional: true,
                success: updateView,
                error: errorInSubscribing
            });
于 2012-11-29T05:27:45.373 に答える
0

コントローラーにデータをクエリ文字列として渡すだけで機能します

 $.ajax({
            type: 'POST',
            url: '/Batch/SetToDispatch/' + unSelected + ',' + batchId
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            traditional: true,
            success: updateView,
            error: errorInSubscribing
        });

カンマ区切りで、コントローラーで分割して使用できます。それが役に立てば幸い

于 2012-11-29T05:10:29.063 に答える