2012-05-10 54 views
0

我使用ASP.NET MVC2來實現谷歌自定義搜索。我能夠連接到googleAPI,傳遞我的查詢,以JSON格式返回結果並將這些結果映射到.NET類。但是,當我創建一個視圖來顯示這些結果時,我正在接收一個錯誤。谷歌自定義搜索使用ASP.NET MVC2

public ActionResult SiteSearch(string query) 
     { 
      var googleSiteSearchClient = new GoogleSiteSearchClient(); 
      var model = googleSiteSearchClient.RunSearch(query); 
      return View(model); 


     } 

我創建基於模型的強類型視圖,並嘗試使用foreach循環得到的結果

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>SiteSearch</h2> 

    <% foreach (var item in Model) { %> 


       <%: item.Kind %> <br /> 

    <% } %> 

    </table> 

</asp:Content> 

我正在receving這個錯誤

模型項目通過了到字典是'AASD2.Models.GoogleAPI.GoogleSearchResponse'類型,但是此字典需要類型爲'System.Collections.Generic.IEnumerable`1 [AASD2.Models.GoogleAPI.GoogleSearchResponse]'的模型項。

任何建議,我在哪裏做錯了?

回答

0

似乎googleSiteSearchClient的結果不是一個集合。搜索結果集合可能是googleSiteSearchClient結果的屬性。

要麼使用

<% foreach (var item in Model.WhatEverTheCollectionIsNamed) { %> 


       <%: item.Kind %> <br /> 

    <% } %> 

或者

public ActionResult SiteSearch(string query) 
     { 
      var googleSiteSearchClient = new GoogleSiteSearchClient(); 
      var model = googleSiteSearchClient.RunSearch(query); 
      return View(model.WhatEverTheCollectionIsNamed);   
     } 
+1

完美,非常感謝 –

+0

現在,我得到返回結果,但頁面只顯示前10個結果,任何想法,其餘結果不見了?我已更新帖子以顯示圖片。 –