2017-02-01 3 views
0

KeyValuePairオブジェクトのリストを含むオブジェクトのリストを、他のいくつかのプロパティと共に渡そうとしています。 WebAPI。しかし、JSONをRest Consoleのようなもの(あるいはJavascriptからのAJAX呼び出しでも)を使って送信すると、オブジェクトのリストが正しく挿入され、List>が正しい数のオブジェクトを取得しますが、 。List <KeyValuePair <string、string >>をWebAPIに渡すとNULL値が返される

これは私がこれをデバッグするために使用しているコードです。

WebApiメソッドを作成してMyModelアイテムのリストを取得しました。そこで、JSONがどのようにそれらを作成しているかを確認できます。私は、追加のプロパティに追加した項目の一覧の配列を作ってみました

[ 
    { 
    "ItemType ":"Item1", 
    "KeyValuePairs":[ 
    { 
     "Key":"MinItems", 
     "Value":"3.0" 
    }, 
    { 
     "Key":"MaxItems", 
     "Value":"11.0" 
    } 
    ], 
    "MinCount ":3000, 
    "MaxCount ":5000 
}, 
{ 
    "ItemType ":"Item2", 
    "KeyValuePairs":[ 
    { 
     "Key":"PaymentMin", 
     "Value":"150" 
    }, 
    { 
     "Key":"PaymentMax", 
     "Value":"500" 
    } 
    ], 
    "MinCount ":1500, 
    "MaxCount ":2500 
    } 
] 

[HttpPost] 
    public JsonResult GetSavedItemsJson(int accountId) 
    { 
     var tmpItem = new List<MyModel>(); 
     tmpItem.Add(new MyModel 
     { 
      ItemType = "Item1", 
      MinCount = 3000, 
      MaxCount = 5000, 
      KeyValuePairs = new List<KeyValuePair<string, string>> 
      { 
       new KeyValuePair<string, string>("MinItems", "3.0"), 
       new KeyValuePair<string, string>("MaxItems", "11.0") 
      } 
     }); 
     tmpItem.Add(new MyModel 
     { 
      ItemType = "Item2", 
      MinCount = 1500, 
      MaxCount = 2500, 
      KeyValuePairs = new List<KeyValuePair<string, string>> 
      { 
       new KeyValuePair<string, string>("PaymentMin", "150"), 
       new KeyValuePair<string, string>("PaymentMax", "500") 
      } 
     }); 
     var result = new JsonResult 
     { 
      MaxJsonLength = int.MaxValue, 
      Data = tmpItem 
     }; 
     return result; 
    } 

これは、次のJSONを私に戻っています。

{ 
"itemName":"MyTestItem", 
"accountId":54321, 
"userId":12345, 
"itemModels":[ 
    { 
    "ItemType":"Item1", 
    "KeyValuePairs":[ 
     { 
      "Key":"MinItems", 
      "Value":"3.0" 
     }, 
     { 
      "Key":"MinItems", 
      "Value":"11.0" 
     } 
    ], 
    "MinCount":3000, 
    "MaxCount":5000 
    }, 
    { 
    "ItemType":"Item2", 
    "KeyValuePairs":[ 
     { 
      "Key":"PaymentMin", 
      "Value":"150" 
     }, 
     { 
      "Key":"PaymentMax", 
      "Value":"500" 
     } 
    ], 
    "MinCount":1500, 
    "MaxCount":2500 
    } 
] 
} 

私はaccountId、ItemNameとuserIdの数値を正しく取得します。 MinCount、MaxCount、およびItemTypeの値を持つリストアイテムも取得します。それは各オブジェクトに対して2つのアイテムを与えるKeyValuePairsリストですが、値はnullです。

上記の値は、WebApiに値を送信するためにREST Consoleを使用したものです。

違いがある場合は、このjavascriptを使用してオブジェクトを作成しました。それは私にまったく同じ結果をもたらします。

function saveItem() { 

var saveData = new Array(); 
var properties = new Array(); 
properties.push({ "MinItems": "3.0" }); 
properties.push({ "MinItems": "11.0" }); 
saveData.push({ ItemType: "Item1", MinCount: 3000, MaxCount: 5000, KeyValuePairs: properties }); 
properties = new Array(); 
properties.push({ "PaymentMin": "50" }); 
properties.push({"PaymentMax": "300" }); 
saveData.push({ ItemType: "Item2", MinCount: 1500, MaxCount: 2500, KeyValuePairs: properties }); 
$.ajax({ 
    method: "POST", 
    url: "/Area1/SomeArea/SaveItem", 
    data: { itemName: "MyTestItem", accountId: 54321, userId: 12345, budgetModels: saveData }, 
    dataType: "json", 
    success: function (data) { 
     debugger; 
     console.log("Data: " + data); 
    }, 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     console.log("Status: " + textStatus); 
     console.log("Error: " + errorThrown); 
    } 
}); 
} 

私は何か間違っている必要がありますが、スタックオーバーフローやその他のサイトでまともな回答を見つけることができません。

答えて

0

ここでこのテストを行いました。私のRequestTestオブジェクトを見てください。

public class RequestTest 
{ 
    public string ItemType { get; set; } 
    public List<KeyValuePair<string,string>> KeyValuePairs { get; set; } 
} 

[HttpPost] 
[ActionName("Test")] 
[Route("api/TestInfo/Test")] 
public IHttpActionResult Test([FromBody]RequestTest requestTest) 
{ 
    Debug.WriteLine(requestTest.ItemType); 
    Debug.WriteLine(requestTest.KeyValuePairs.First().Key); 
    Debug.WriteLine(requestTest.KeyValuePairs.First().Value); 

    return Ok(); 
} 



function saveItem() { 
    debugger; 

    var properties = new Array(); 
    properties.push({ "Key": "MaxItems", "Value": "3.0" }); 
    properties.push({ "Key": "MinItems", "Value": "11.0" }); 

    var requestTest = {}; 
    requestTest.ItemType = "ItemString"; 
    requestTest.KeyValuePairs = properties; 

    $.ajax({ 
     method: "POST", 
     url: apiURL + '/TestInfo/Test', 
     data: requestTest, 
     dataType: "json", 
     success: function (data) { 
      console.log("Data: " + data); 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
      console.log("Status: " + textStatus); 
      console.log("Error: " + errorThrown); 
     } 
    }); 
} 

enter image description here

+0

私は同じ結果と同様にすることを試みました。私はちょっとしたコードが重くなってしまうのではないかと心配していたので、最初の質問から外しました。 – SpaceCowboy74

+0

ここではテストしました。確認できますか? –

+0

奇数。それはまだ私のために働いていない。 Mineは、KeyValuePair項目のリストを持つ項目のリストです。たとえば、上記のサンプルのrequestTestはList です。 – SpaceCowboy74

関連する問題