2012-10-10 31 views
2

我正在研究一些高級搜索功能,並遇到了障礙。我想爲我的搜索結果使用AJAX,並使用PagedList添加來處理分頁。我有一個AdvancedSearchResults操作,它採用搜索過濾器類和當前頁面。問題是我如何正確地將搜索過濾器類傳遞到此操作中。我相信我正在做的事情沒有正確組織,因爲我認爲這很常見。我真的很感激任何幫助。提前致謝。Ajax分頁列表傳遞對象到控制器

控制器動作:

public ActionResult AdvancedSearchResults(AdvancedSearchFilters searchFilters, int ? page) 
    { 
     //DO STUFF 

     return PartialView("_SearchResults", results); 
    } 

AdvancedSearchResults過濾器:

public class AdvancedSearchFilters 
{ 
    public string SearchText { get; set; } 
    public List<string> SelectedTableTypes { get; set; } 
    public List<Guid> SelectedGenreIds { get; set; } 
    public List<Guid> SelectedPlatformIds { get; set; } 
    public int YearMax { get; set; } 
    public int YearMin { get; set; } 
    public int RatingMin { get; set; } 
    public int RatingMax { get; set; } 
} 

搜索結果管窺:

@model List<SearchResultItem> 

@using PagedList 
@using PagedList.Mvc 

<!-- 15 Per Page --> 
<h2>Search Results</h2> 
@if (ViewBag.OnePageOfSearchItems.Count > 0) 
{ 
    <div class="pagination"> 
     @Html.PagedListPager((IPagedList)ViewBag.OnePageOfSearchItems, page => Url.Action("AdvancedSearchResults", "Search", new { searchFilters = ViewBag.AdvancedSearchFilters, page = page }), 
           PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.OnlyShowFivePagesAtATime, "#main-results")) 
    </div> 
    foreach (var searchResultItem in Model) 
    { 
     <a href="@Url.Action(searchResultItem.ResultType, searchResultItem.ResultType, new { id = searchResultItem.ResultId })" class="result"> 
      @if (searchResultItem.ProfileImageLocation != null) 
      { 
       <img src="@searchResultItem.ProfileImageLocation" alt="@searchResultItem.ResultName" /> 
      } 
      <div class="result-info"> 
       <h3>@searchResultItem.ResultName</h3> 
       <p>@searchResultItem.DisplayText</p> 
      </div> 
     </a> 
    } 
    <div class="pagination"> 
     @Html.PagedListPager((IPagedList)ViewBag.OnePageOfSearchItems, page => Url.Action("AdvancedSearchResults", "Search", new { searchFilters = ViewBag.AdvancedSearchFilters , page = page }), 
           PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.OnlyShowFivePagesAtATime, "#main-results")) 
    </div> 
} 
else 
{ 
    <span>No Matches.</span> 
} 
+0

你用什麼來獲取你的數據? (對於exmaple實體框架,NHibernate,數據集?) – Kamyar

+0

我正在使用實體框架,但由於搜索可以檢索多種類型的數據,我必須製作一個更通用的模型來存儲它。 –

回答

0

嘗試將數據傳遞到操作方法以JSON形式。嘗試下面的代碼,將json對象傳遞給action方法。

//// construct your json object. 
var jsonObj = '{"SelectedTableTypes" : [{"1","2"}]}'; 


var postData = $.parseJSON(jsonObj); 


$.ajax(
    { 
     url: // your URL with Action name AdvancedSearchResults, 
     data: postData, 
     cache: false, 
     dataType: "json", 
     success: function (result) { 

     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
           alert(xhr.status); 
           alert(thrownError); 
     } 
    }); 
相關問題