Ajax分页列表将对象传递给控制器

时间:2012-10-10 06:05:20

标签: asp.net-mvc-3 asp.net-ajax pagedlist

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

控制器操作:

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

1 个答案:

答案 0 :(得分:0)

尝试以json的形式将数据传递给action方法。尝试下面的代码,它将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);
       }
   });