2016-06-29 130 views
0

我有一個頁面,需要將來自客戶端的複雜數據發送到asp.net mvc中的webmethod。 我的數據是:如何將複雜類型從客戶端發送到服務器端

{"tbl":[{"row":{"items":[{"name":"madrak1","val":"fdsfds"},{"name":"mahaleTahsil1","val":"fdsfds"},{"name":"reshte1","val":""},{"name":"start1","val":""},{"name":"end1","val":""}]}}]} 

我需要有一個名爲類表中把所有的變量裏面。 我的webmethod是:

public ActionResult SaveDetailedInfo(List<rrow> tbl) 
    { 
     return Json(new { status = "Success", message = "Success" }); 
    } 
+0

您可以使用字符串代替List。 –

回答

0

你的C#類應該是這個樣子:

public class Item 
{ 

    [JsonProperty("name")] 
    public string Name { get; set; } 

    [JsonProperty("val")] 
    public string Val { get; set; } 
} 

public class Row 
{ 

    [JsonProperty("items")] 
    public IList<Item> Items { get; set; } 
} 

public class Tbl 
{ 

    [JsonProperty("row")] 
    public Row Row { get; set; } 
} 

public class Table 
{ 

    [JsonProperty("tbl")] 
    public IList<Tbl> Tbl { get; set; } 
} 

而且你的MVC方法:

public ActionResult SaveDetailedInfo(Table table) 
{ 
    return Json(new { status = "Success", message = "Success" }); 
} 

所以從前端發送數據...

var table = { 
    "tbl": [{ 
     "row": { 
      "items": [{ 
       "name": "madrak1", 
       "val": "fdsfds" 
      }, { 
       "name": "mahaleTahsil1", 
       "val": "fdsfds" 
      }, { 
       "name": "reshte1", 
       "val": "" 
      }, { 
       "name": "start1", 
       "val": "" 
      }, { 
       "name": "end1", 
       "val": "" 
      }] 
     } 
    }] 
}; 

var xhr = new XMLHttpRequest(); 
xhr.open('POST', '/Home/SaveDetailedInfo', true); 
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); 

// send the collected data as JSON 
xhr.send(JSON.stringify(table)); 

應導致...

enter image description here

PS。 There is an easy way to convert your json objects into C# classes

相關問題