2012-11-29 5 views
1

jQuery Ajax 게시물을 사용하여 컨트롤러 작업에 요소 배열을 게시하려면 어떻게해야합니까?ajax 게시물을 사용하여 MVC3 컨트롤러에 배열 게시

이것은 내가 시도하는 방법이지만, 내 컨트롤러는 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); 
} 

누군가가 내가 놓친 거지 어떻게 저를 조언 할 수 내 컨트롤러

입니다. 당신은 단순히 쿼리 문자열로 컨트롤러에 데이터를 제공 할 수 있습니다

+0

제거 피곤 되세요 "[]"allocationId – ankur

+0

후'unSelected' 선언을 보여줄 수 있습니까? – testCoder

답변

0

귀하의 JSON 구문은 오프 더 [] allocationId은 [] 참조 후 필요 없을 것 같다 작동합니다

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

쉼표로 구분하여 컨트롤러로 분리하여 사용할 수 있습니다. 는

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 
      }); 
관련 문제