2010-06-13 58 views
1

我需要能夠提供最終用戶的頁面中,大公可以在下面的搜索:如何使用MVC中的參數進行搜索?

  • 搜尋工作
  • 類別
  • 價格
  • ADTYPE
  • 位置

等等。

重要的是用戶可以複製網址,然後稍後使用(並獲得相同的設置)。能夠將這些設置保存在數據庫中的用戶中也很重要。

到目前爲止,我創建了一個包含參數的ModelView類,但我不確定這是正確的方法嗎?馬比,我應該通過所有的網址。

如果是這樣,我該如何做到這一點?有我可以看的樣品嗎?

BestRegards

回答

3

問題最少的方式是通過在查詢字符串的參數。

mysite.com/search/?c=1&p=1.00&at=blah&location=75081&..... 

這種方法沒有什麼非MVC。實際上,當傳遞不與模型字段相對應的參數時,您可能會看到使用QueryString參數生成的ActionLinks。

你就可以創建

Search/{Category}/{Price}/{AdType}/{Location}/... 

的路線,但是,在我看來這是錯誤的路要走。我假設可能不正確,使用路由會使URL更加適合搜索引擎,但這只是一個問題,如果您希望不同的搜索URL組合可以被外部搜索引擎發現,而這種搜索引擎真的不會產生太多感覺作爲一個網站地圖。

更新:

如果你宣佈你的行動,像這樣:

public ActionResult Search(string category, float price, string adType, float location) 
{ 
/// do whatever you want in here 
} 

那麼你的queryString參數將被映射到該動作方法的參數。

所有你需要做的是設置你的表單「方法」來獲得,

<form method="GET" ....> 

讓您的字段的查詢字符串參數,而不是形式的POST數據傳遞。

+0

所以你在說什麼,我應該構建一些queryString提交,將我的所有參數都考慮在內。然後在服務器端,我將extraxt quaryString並驗證它與注入攻擊。如果這是有效的,我能夠將正確的數據返回給客戶端? 我應該使用一個queryString,並且只包含選擇的參數,還是應該使用路由? 我該如何構建查詢字符串儘可能簡單? – Banshee 2010-06-13 15:19:48

+0

將表單方法設置爲「GET」將導致您的表單字段作爲queryString參數而不是POST數據傳遞。您需要在操作方法中驗證服務器上的數據。如果您正在搜索數據庫,請使用存儲過程執行搜索,並使用像Enterprise Library這樣的好庫來調用存儲過程,因爲這將有助於防止注入。請參閱上面的updat。 – 2010-06-13 15:27:36

+0

謝謝,但這是否意味着我將不得不在客戶端上提交JavaScript函數的某種類型時手動生成查詢字符串?你maby有一個關於如何生成這個querystring的例子嗎? – Banshee 2010-06-13 18:47:11

0

對不起,但評論字段是小的使用。

的問題是,我的webmethod是這樣的:

[AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult List(AdList adList) 
     { 
      try 
      { 
       return View(adList); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

的AdList看起來是這樣的:

public class AdList 
    { 
     public AdList() 
     { 
      this.ModelViewAd = new List<ModelViewAdList>(); 
     } 

     public List<ModelViewAdList> ModelViewAd { get; set; } 
     public SeartchParameters ModelViewSeartchParameters { get; set; } 
    } 

這是成員的樣子:

public class SeartchParameters 
    { 
     public string SeartchString { get; set; } 
     public int CategoryId { get; set; } 
     public int MunicipalityId { get; set; } 
     public int CountryId { get; set; } 
    } 
    public class ModelViewAdList 
    { 
     [DisplayName("Titel")] 
     public string Titel { get; set; } 

     [DisplayName("Köp ny pris")] 
     public Decimal BuyNowPrice { get; set; } 

     [DisplayName("Reservations pris")] 
     public Decimal ReservationPrice { get; set; } 

     [Required(ErrorMessage = "Annons text saknas")] 
     [DisplayName("Annons text")] 
     public string Description { get; set; } 

     [DisplayName("Slutdatum")] 
     public DateTime? EndDate { get; set; } 

     [DisplayName("Frakt kostnad")] 
     public Decimal Shipping { get; set; } 

     [DisplayName("Produktens Skick")] 
     public ProductStatus StateOfProduct { get; set; } 

     [DisplayName("Start pris")] 
     public Decimal StartingPrice { get; set; } 

     [DisplayName("Annonstyp")] 
     public RadioButtonListViewModel<AdType> TypeOfAd { get; set; } 
    } 

如果我刪除AdList作爲參數,然後廣告seartch參數,而不是它可能無法工作因爲MVC試圖發回大量的對象?

那麼我如何結合David的例子來解決這個問題呢?