2

我正在开发一些高级搜索功能,但遇到了障碍。我想为我的搜索结果使用 AJAX,并使用 PagedList 插件来处理分页。我有一个 AdvancedSearchResults 操作,它采用搜索过滤器类和当前页面。问题是如何正确地将搜索过滤器类传递到此操作中。我确信我正在做的事情没有正确组织,因为我认为这将是相当普遍的。我真的很感激任何帮助。提前致谢。

控制器动作:

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

      return PartialView("_SearchResults", results);
  }

高级搜索结果过滤器:

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>
}
4

1 回答 1

0

尝试以 json 的形式将数据传递给 action 方法。尝试以下将 json 对象传递给操作方法的代码。

//// 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);
       }
   });
于 2012-10-10T07:02:30.340 回答