2016-09-26 67 views
0

我發佈以下對象和我的控制器頁面獲取字符串格式如何在c#中轉換序列化對象。如何將對象轉換爲MVC控制器中的序列化對象

[HttpPost] 
    public object StoreCheckList(object ChkList) 
    { 

    } 

[{"Remarks": "Teat","CountryId": 1,"ClientId": 1,"FacilityId": 1,"SpaceId":2},{"Remarks": "Teat","CountryId": 1,"ClientId": 1,"FacilityId":1,"SpaceId": 5}] 

回答

0

使用Newtonsoft.Json,就像這樣:

YourObjectType obj = JsonConvert.DeserializeObject<YourObjectType>(json); 
3

您可以使用內置支持強類型的模型綁定MVCS的。首先,創建具有與輸入對象

例如

public class StoreCheckListModel { 
    public string Remarks {get;set;} 
    public int CountryId {get;set;} 
    public int ClientId {get;set;} 
    public int FacilityId {get;set;} 
    public int SpaceId {get;set;} 
} 

然後改變你的MVC行動:

[HttpPost] 
public object StoreCheckList(StoreCheckListModel[] ChkList) { 

} 
0

,顧名思義,是MVC模型 - 視圖 - 控制器。你的問題是關於如何在控制器中處理我發佈的請求。

但是在處理控制器之前,您必須確保您的ModelView的設計符合此模式。

看起來您並未使用Model。首先,創建一個如下所示的模型。

public class MyModel 
{ 
    public string Remarks { get; set; } 
    public int CountryId { get; set; } 
    public int ClientId { get; set; } 
    public int FacilityId { get; set; } 
    public int SpaceId { get; set; } 
} 

改變你這樣的控制器的方法,

[HttpPost] 
public object StoreCheckList(IEnumerable<MyModel> chkList) 
{ 
    List<MyModel> myCheckList = chkList.ToList(); 
    ///do what you want to do 
} 

這應該工作,因爲它是沒有任何系列化相關代碼。