3

JSON を ASP.NET のコントローラー関数に送信すると、JSON は次のようになります。

[{"Key":"Test23.txt","Size":6,"LastModified":"Thu, 01 Nov 2012 19:35:43 GMT"},
{"Key":"Test25.txt","Size":6,"LastModified":"Wed, 31 Oct 2012 21:02:51 GMT"},
{"Key":"Test28.txt","Size":6,"LastModified":"Thu, 01 Nov 2012 19:35:42 GMT"}]

MVCコントローラーメソッドでそれをどのように受け入れ、どのように解析しますか?

ありがとうございました。

4

3 に答える 3

2

一致する場合、Mvc はこれを viewModel に自動的にバインドします。

public ActionResult SaveFiles(List<FileViewModel> filesViewModel){

}

FileViewModel は次のようになります

public class FileViewModel
{
  public string Key {get;set}
  public int Size {get;set}
  public DateTime LastModified {get;set;}
}
于 2012-11-02T15:57:33.563 に答える
1

デフォルトのモデル バインダーは、これらすべてを実行できます。サードパーティのライブラリを使用する必要はありません。コレクションをアクションに渡す場合、それらはインデックス可能である必要があります。したがって、arrayorを使用する必要がありListます。

// First, define your input model.
public class MyModel
{
  public string Key { get; set; }
  public int Size { get; set; }
  public string LastModified { get; set; }
}

// NExt, use that model in your action.
public ActionResult MyAction(MyModel[] things)
{
}

そして、すべてがあなたのために配線されているはずです。

于 2012-11-02T16:00:09.010 に答える
0
public class Item
{
  public string Key {get;set;}
  public int Size {get;set;}
  public DateTime LastModified {get;set;}
}

public ActionResult MyAction(List<Item> items)
{

}
于 2012-11-02T15:59:31.000 に答える