2017-02-21 64 views
-1

我在JavaScript中有這個Json數據,我想在C#中使用完全相同的數據,我如何在C#中格式化此數據。在C中格式化Json數據#

let header = '[{"data": [{"id": "Id","name": "Full Name","age": "Age"}]}]'; 

這是我在MVC控制器:

 public ActionResult Index() 
    { 
     User user = new User(); 



     user.GridHeader = "[{data: [{id: Id,name: Full Name,age: Age}]}]"; 
     user.GridData = "[{data: [{id: 1,name: Sam,age: 32}, {id: 2,name: tom,age: 22}, {id: 3,name: mina,age: 25}]}]"; 

     return View(user); 
    } 

我需要GridHeader相同的格式,標題

+2

創建一個模型並綁定它...你使用什麼框架MVC,WebAPI,Winforms? –

+0

@ johnny它使用MVC,我已經添加了更多的解釋給我的問題。 – Alma

+0

你的「header」變量是一個字符串,而不是JSON/Javascript對象。它恰好看起來像JSON。那是你要的嗎? – ADyson

回答

0

你需要一個模型綁定到

public class UserDTO 
{ 
    public UserData[] Data {get; set;} 
} 

public class UserData 
{ 
    public int Id { get; set} 
    public string Name { get; set;} 
    public int Age { get; set; } 
} 

你或許應該做在網格中靜態標題,所以你不必每次都發送它,但如果你不只是發送它作爲fi第一個記錄。

public ActionResult Index(UserDTO user) 
{ 
    // your user data is in user.Data 
    return View(user); 
} 

你應該查找模型綁定是如何工作的。這裏是好的resource