2011-04-26 67 views
6

我有一個控制器函數,以前有整數作爲URL中的每個部分(我在路由文件中設置),但現在其中一個參數需要是一個整數數組。這裏是控制器動作:在asp.net mvc中,我如何傳遞一個整數數組作爲參數

public JsonResult Refresh(string scope, int[] scopeId) 
    { 
     return RefreshMe(scope, scopeId); 
    } 
在我的JavaScript

,我有以下的,但我現在需要得到scopeId是一個整數數組。 我怎樣才能設置一個URL張貼到使用jquery,JavaScript的

var scope = "Test"; 
    var scopeId = 3; 

    // SCOPEID now needs to be an array of integers 

    $.post('/Calendar/Refresh/' + scope + '/' + scopeId, function (data) { 
     $(replacementHTML).html(data); 
     $(blockSection).unblock(); 
    } 

回答

12

下應該做的工作:如果您使用ASP.NET MVC 3,你也可以發送請求作爲JSON對象

var scope = 'Test'; 
var scopeId = [1, 2, 3]; 

$.ajax({ 
    url: '@Url.Action("Refresh", "Calendar")', 
    type: 'POST', 
    data: { scope: scope, scopeId: scopeId }, 
    traditional: true, 
    success: function(result) { 
     // ... 
    } 
}); 

和:

$.ajax({ 
    url: '@Url.Action("Refresh", "Calendar")', 
    type: 'POST', 
    contentType: 'application/json; charset=utf-8', 
    data: JSON.stringify({ scope: scope, scopeId: scopeId }), 
    success: function(result) { 
     // ... 
    } 
}); 
0

下面是從jquery的API爲$ .POST()

實施例的一個示例:通過數據的陣列到服務器(而仍然忽略返回結果)。

$.post("test.php", { 'choices[]': ["Jon", "Susan"] }, function() { //do stuff }); 
+2

我不不要認爲'[]'中的'[]'是必需的。 – Omar 2011-04-26 03:49:09

+0

@Omar這是jQuery網站上的例子。只是一個剪切/粘貼工作。 http://api.jquery.com/jQuery.post/ – 2011-04-26 03:52:15

+0

@Omar:你是對的,這不是必需的。 – 2011-04-26 04:45:42