2015-01-14 58 views
1

我試圖從SmartTable中完成當前tableState的asp.net映射。AngularJS SmartTable&ASP.NET映射錯誤

我使用ASP.NET MVC 5.

我angularjs調用這個樣子的:

usersService.getUsers({ 
       start: start, 
       number: number, 
       tableState: tableState 
      } 
     ).then(function(result) { 

     }); 

我的queryString參數是這樣的:

number:10 
start:0 
tableState:{"sort":{"predicate":"location","reverse":false},"search":{"predicateObject":{"location":"gfdgd","service":"gfdgd","company":"gd","fullname":"john"}},"pagination":{"start":0,"number":10}} 

,當然我試着在asp.net中查詢查詢信息。我的控制器操作是這樣的:

public JsonResult GetUsers(SmartTableRequestModel request) 
     { 
      return GetJsonResult(new 
      { 

      }); 
     } 

而我試圖在綁定的模式有以下幾種:

public class SmartTableRequestModel 
    { 
     public SmartTableQueryModel TableState { get; set; } 

     public int Number { get; set; } // number is well-binded 

     public int Start { get; set; } // the start is well-binded too 
    } 

public class SmartTableQueryModel 
    { 
     [JsonProperty("sort")] 
     public SmartTableSortModel Sort { get; set; } 

     [JsonProperty("search")] 
     public SmartTableSearchModel Search { get; set; } 

     [JsonProperty("pagination")] 
     public SmartTablePaginationModel Pagination { get; set; } 
    } 

public class SmartTableSortModel 
    { 
     [JsonProperty("predicate")] 
     public string Predicate { get; set; } 

     [JsonProperty("reverse")] 
     public bool Reverse { get; set; } 

    } 

等等......

,但我沒有得到在tableState模型中映射的任何東西。我的SmartTableRequestModel對象中的TableState屬性始終爲空。

感謝您的幫助下, 勒茲萬

+0

我假設你使用的是asp.net MVC。你可以請張貼更多的細節,比如你正在使用什麼版本的MVC? –

+0

我更新了我的問題。對不起。我使用ASP.NET MVC 5. –

+0

您是否嘗試過使用TypeConverters或模型聯編程序?請參閱http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api – mentat

回答

0

問題是我的getUsers是一個httpget方法,你不能將模型傳遞給一個httpget方法。

所以我改變了我的資源,以匹配一個httppost調用。

{ 
     getUsers: { url: ci.domainPub + '/Management/GetUsers', method: 'POST', action: 'getUsers'}, 
    }, 

並在控制器的動作將是:

[HttpPost] 
public JsonResult GetUsers(SmartTableRequestModel request) 
     { 
      return GetJsonResult(new 
      { 

      }); 
     } 

有了這個機制,你可以基於在服務器端發佈smartTable插件的tableState未經過任何處理和過濾器/訂單。

0

嘗試發送請求數據類似下面。

{"request" :{"TableState": {"sort":{"predicate":"location","reverse":false},"search":{"predicateObject":{"location":"gfdgd","service":"gfdgd","company":"gd","fullname":"john"}},"pagination":{"start":0,"number":10}}},"Number":10,"Start":1} 
+0

這當然不起作用。綁定不是那樣工作的。 –

+0

你有沒有試過或只是猜測? –

+0

我試過了,但我確信它不會起作用。問題是我的getUsers是一個httpget方法,你不能將模型傳遞給httpget方法,並期望它映射。 –