在asp.net mvc中对Ajax View进行分页,排序和搜索

时间:2015-10-25 05:48:05

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

我正在使用asp.net mvc 4& EF 6建立一个网站,我想要一个填充的表格,用于分页和排序。通过Ajax过滤/搜索PartialView。到目前为止,分页,排序和搜索功能正常工作,但我不能把它带到Ajax,表只会更新而不是页面重新加载。这是我的代码,

控制器

public PartialViewResult Flats(string sortOrder, string currentFilter, string strSearch, int? page)
{
    ViewBag.currentSort = sortOrder;
    if (strSearch != null)
    {
        page = 1;
    }
    else
    {
        strSearch = currentFilter;
    }
    ViewBag.CurrentFilter = strSearch;

    ViewBag.TitleSort = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
    var FlatsOrder = from f in rentdb.FlatInfoes select f;
    if (!String.IsNullOrEmpty(strSearch))
    {
        FlatsOrder = FlatsOrder.Where(s => s.address_area.Contains(strSearch));
    }
    switch (sortOrder)
    {
        case "title_desc":
            FlatsOrder = FlatsOrder.OrderByDescending(a => a.title);
            break;
        default:
            FlatsOrder = FlatsOrder.OrderBy(a => a.title);
            break;
    }
    int pageSize = 5;
    int pageNumber = (page ?? 1);
    return PartialView(FlatsOrder.ToPagedList(pageNumber, pageSize));
}

查看

<div id="divTable" class="span12" style="background-color: #fff;">
    <table class="table table-hover">
        <thead style="background-color: #cccccc;">
            <tr>
                <th class="text-center">
                    @Ajax.ActionLink("Title", "Flats", new { sortOrder = ViewBag.TitleSort, currentFilter = ViewBag.CurrentFilter }, new AjaxOptions() {
                                            HttpMethod = "GET",
                                            UpdateTargetId = "divTable",
                                            InsertionMode = InsertionMode.Replace
                                        })
                </th>
            </tr>
        </thead>
    </table>
</div>

每当我点击Title链接进行排序时,整页重新加载然后排序。我怎样才能通过ajax和partialview更新表格?非常需要这个帮助。感谢。

1 个答案:

答案 0 :(得分:0)

您必须制作包含PartialView的普通视图,如下所示

查看

@model YourModelClass

@* some thing which will not effected when the Ajax request is done *@
@Html.Partial("_TablePartialView", Model)

部分视图,在本例中应命名为“_TablePartialView”,并且应存在于“Views”文件夹内的“Shared”文件夹中。

@model YourModelClass

<div id="divTable" class="span12" style="background-color: #fff;">
  <table class="table table-hover">
    <thead style="background-color: #cccccc;">
        <tr>
            <th class="text-center">
                @Ajax.ActionLink("Title", "AjaxFlats", new { sortOrder = ViewBag.TitleSort, currentFilter = ViewBag.CurrentFilter }, new AjaxOptions() {
                                        HttpMethod = "GET",
                                        UpdateTargetId = "divTable",
                                        InsertionMode = InsertionMode.Replace
                                    })
            </th>
        </tr>
    </thead>
  </table>
</div>

<强>控制器

private YourModelClass GetModel(string sortOrder, string currentFilter, string strSearch, int? page)
{
   ViewBag.currentSort = sortOrder;
   if (strSearch != null)
   {
      page = 1;
   }
   else
   {
      strSearch = currentFilter;
   }
   ViewBag.CurrentFilter = strSearch;

   ViewBag.TitleSort = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
   var FlatsOrder = from f in rentdb.FlatInfoes select f;
   if (!String.IsNullOrEmpty(strSearch))
   {
       FlatsOrder = FlatsOrder.Where(s => s.address_area.Contains(strSearch));
   }
   switch (sortOrder)
   {
      case "title_desc":
         FlatsOrder = FlatsOrder.OrderByDescending(a => a.title);
         break;
       default:
         FlatsOrder = FlatsOrder.OrderBy(a => a.title);
         break;
   }
   int pageSize = 5;
   int pageNumber = (page ?? 1);
   return FlatsOrder.ToPagedList(pageNumber, pageSize);

}

public ActionResult Flats(string sortOrder, string currentFilter, string strSearch, int? page)
{
     return View(GetModel(sortOrder, currentFilter, strSearch, page);
}

public ActionResult AjaxFlats(string sortOrder, string currentFilter, string strSearch, int? page)
{
     return PartialView("_TablePartialView", GetModel(sortOrder, currentFilter, strSearch, page);
}