0

JSON パラメータを asp.net C# に渡しています。パラメータは単一または複数のデータです。

そのため、パラメーターを LIST クラス型として受け取るコードを記述します。

しかし、 「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というエラーが返されます。

このようなテストコードを書くと、同じエラーが発生します。

私のコードを見直して、私にアドバイスしてください。

テストコード、

コントローラークラスで

[HttpGet]
public ActionResult modelTest(TestList obj)
{

    return Content(obj.wow.Count.ToString());
}

モデルとリストクラス、

public class TestList
{
    public List<TestModel> wow { get; set; }
}

public class TestModel
{
    public string id { get; set; }
    public string name { get; set; }
}

/Test/modelTest/?id=myId&name=john&age=11を呼び出します

その後、エラーが発生し、

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 338:        {
Line 339:            
Line 340:            return Content(obj.wow.Count.ToString());
Line 341:        }
Line 342:
4

2 に答える 2

3

wowリストはおそらく初期化されていません。

(たとえば) TestList コンストラクターで実行できます。

public class TestList
{
   public TestList() {
      wow = new List<TestModel>();
   }
    public List<TestModel> wow { get; private set; }//if you do this way, you can have a private setter
}

または、パブリックセッターが必要な場合

public class TestList {

    private List<TestModel> wow_;

    public List<TestModel> wow {
       get {
          if (wow_ == null) wow_ = new List<TestModel>();
          return wow_;
       }
       set {wow_ = value;}
    }
 }
于 2012-07-20T13:54:03.527 に答える
0

あなたの通話システムは正しくないと思います。jsonデータのリストのような「TestList」を追加する必要があります。それをデバッグし、「modelTest」メソッドによって実際に何が受信されるかを確認します。

于 2012-07-20T14:13:43.443 に答える