2014-07-06 51 views
0

我使用SyncFusion Asp.Net MVC網格,在此我想在服務器端進行過濾和JSON發送到服務器,如下Asp.net MVC5 JSON值I結合scafolding

但在視圖模型,其中對象propertis來了爲空沒有約束力

的Json

{"select":["Area","Id"],"where":[{"isComplex":false,"field":"Area","operator":"startswith","value":"test","ignoreCase":true}],"sorted":[{"name":"Area","direction":"ascending"}]} 

我已經創建的模型如下,這是傳遞給控制器​​,但它不具有約束力。

public class UserViewModel 
    { 
     public int skip { get; set; } 
     public int take { get; set; } 
     public Sort sorted { get; set; } 
     public string[] group { get; set; } 
     //Grid Action Params; 
     public string action { get; set; } 
     public string key { get; set; } 
     public string keyColumn { get; set; } 
     public string[] select { get; set; } 
     public Search search { get; set; } 
     public Where where { get; set; } 
     public ApplicationUser value { get; set; } 
    } 

    public class Where 
    { 
     public bool isComplex { get; set; } 
     public string field { get; set; } 
     public string @operator { get; set; } 
     public string @value { get; set; } 
     public bool ignoreCase { get; set; } 

    } 
    public class Sort 
    { 
     public string name { get; set; } 
     public string direction { get; set; } 
    // "sorted":[{"name":"Area","direction":"ascending"}],"group":["Area"] 
    } 

    public class Search 
    { 
     public string[] fields { get; set; } 

     public string @operator { get; set; } 

     public string key { get; set; } 

     public bool ignoreCase { get; set; } 
    } 

控制器方法

public async Task<ActionResult> DataSource(UserViewModel editParams) 
    { 

    } 

回答

0

您發送似乎並沒有在所有符合您的視圖模型的JSON:

{ 
    "select": [ 
     "Area", 
     "Id" 
    ], 
    "where": [ 
     { 
      "isComplex": false, 
      "field": "Area", 
      "operator": "startswith", 
      "value": "test", 
      "ignoreCase": true 
     } 
    ], 
    "sorted": [ 
     { 
      "name": "Area", 
      "direction": "ascending" 
     } 
    ] 
} 

考慮編寫,將匹配該結構的視圖模型:

public class UserViewModel 
{ 
    public string[] Select { get; set; } 
    public Where[] where { get; set; } 
    public Sorted[] sorted { get; set; } 
} 

public class Where 
{ 
    public bool IsComplex { get; set; } 
    public string Field { get; set; } 
    public string Operator { get; set; } 
    public string Value { get; set; } 
    public bool IgnoreCase { get; set; } 
} 

public class Sorted 
{ 
    public string Name { get; set; } 
    public string Direction { get; set; } 
} 

現在你的控制器動作可以採取這種觀點模型參數:

public async Task<ActionResult> DataSource(UserViewModel editParams) 
{ 
    ... 
} 

我不熟悉SyncFusion Asp.Net MVC grid,你似乎可以用,但你應該確保該Content-Type: application/json請求HTTP標頭一起發送AJAX請求,以便ASP.NET MVC模型聯編程序知道從客戶端發送的內容類型。使用Web瀏覽器的開發人員工具欄或Fiddler等工具檢查客戶端發送的請求,並確保該標題存在。否則,視圖模型將不會被綁定。