2013-02-28 32 views
0

我的動作接受整數值的數組:如何使用Ajax獲取整數值數組到一個Action?

public ActionResult ActionName(int firstParamId, int secondParamId, int[] contactIds) 

我使用下面的代碼但整數選定陣列不發送過來。

function btnSend_onclick() { 

      var firstParamId = 1; 
      var secondParamId= 2; 
      var selectedContactIds = []; 

      $('#ContactsListContainer input:checked').each(function() { 
       selectedContactIds.push($(this).val()); 
      }); 

      var params = { 
       firstParamId : firstParamId , 
       secondParamId: secondParamId, 
       contactIds: selectedContactIds 
      }; 

      $.get('/ControllerName/ActionName', JSON.stringify(params)) 
       .done(function(data, status) { 
        alert("Data: " + data + "\nStatus: " + status); 
       }) 
       .fail(function(data) { 
        debugger; 
        alert(data.responseText); 
       }); 
     } 

我收到下面的錯誤每個參數:

The parameters dictionary contains a null entry for parameter 'firstParamId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Actionname(Int32, Int32, Int32[])' 
in 'AppNamespace.MvcUI.Controllers.ControllerName'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.<br>Parameter name: parameters 

出了什麼問題上面的代碼?

回答

0
try this , 

    var param={ 
     contactIds:[] 
    }; 

     while pushing do param.contactIds.push(...); 

     $.get('/ControllerName/ActionName', 
     {     
       "param": JSON.stringify(param) 
     }) 
     .done(function(data, status) { 
       alert("Data: " + data + "\nStatus: " + status); 
     }) 
     .fail(function (data, status) { 
       alert("Data: " + data + "\nStatus: " + status); 
     }); 

這裏,把一個全局變量中,我創建的數組,然後我就在array.Then所有的值上,雖然sending.It爲我工作使用JSON.stringify(PARAM)。

在行動,

String param = (String) request.getParameter("param"); 
JSONObject searchCriteriaJSONObj = (JSONObject) JSONSerializer.toJSON(param); 


String paramStr = searchCriteriaJSONObj.getString("contactIds"); 
List<JSONObject> skillsJSONObj = (List<JSONObject>) JSONArray.fromObject(paramStr); 

它是在Struts2,在struts1中你可能需要你怎麼設置contactIds PARAM值使用插件JSON轉換

+0

? – 2013-02-28 11:24:54

+0

如果你有解決方法,請將它標記爲答案:) – PSR 2013-02-28 11:26:24

+0

它沒有爲我工作。 – 2013-02-28 11:37:50

相關問題