0

私のアクションは整数値の配列を受け入れます:

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

上記のコードの何が問題になっていますか?

4

1 に答える 1

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);
        });

ここで、作成した配列でグローバル変数を取得し、その配列にすべての値をプッシュしました。次に、送信中に 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 では、jSON 変換にプラグインを使用する必要がある場合があります。

于 2013-02-28T11:21:58.983 に答える