MVC 5排序和过滤数据

时间:2015-01-22 16:56:33

标签: javascript c# asp.net-mvc asp.net-mvc-5 asp.net-ajax

在我看来,我有可能使用返回部分视图的Ajax.BeginForm来过滤显示的数据,并仅刷新包含数据的表。使用Ajax.ActionLink单击列标题时,我也可以按列排序。问题在于,当我使用Ajax.ActionLink时,我必须刷新整个页面并且松开在过滤器中选择的内容(它们是自定义多选组合控件),因此整个数据被排序和显示。

我尝试使用ViewBag发送Json数据以阻止过滤器,但我失败了。我宁愿只在排序时刷新表。我是MVC的新手。 如何以一种很好的方式做到这一点?谢谢你的帮助!

查看:

@using (Ajax.BeginForm(new AjaxOptions() {HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "exposureRecords"}))
{
    <div class="form-group">
        <select id="brands-select" name="brands" multiple="multiple">
            @{
                var brands = Model.Brands;

                foreach (var item in brands)
                {
                    <option value="@item">@item</option>
                }
            }
        </select>
    </div>
    (...similar for 3 other filters...)
    <table class="table">
        <tr>
            <th>
               @Html.ActionLink("Marka", "Index", new { sortOrder = ViewBag.BrandSortParam })
            </th>
            <th>
               @Html.ActionLink("Dyscyplina", "Index", new { sortOrder = ViewBag.DisciplineSortParm })
            </th>
            <th>
                @Html.ActionLink("Miesiąc", "Index", new { sortOrder = ViewBag.MonthSortParm })
            </th>
            <th>
                @Html.ActionLink("Liczba ekspozycji", "Index", new { sortOrder = ViewBag.QuantitySortParm })
            </th>
            <th>
                @Html.ActionLink("Ekwiwalent reklamowy", "Index", new { sortOrder = ViewBag.ValueSortParm })
            </th>
        </tr>
    </table>
}
@Html.Partial("_Table",Model)

然后在控制器中我有

   public ActionResult Index(IEnumerable<string> brands, IEnumerable<string> disciplines,
        IEnumerable<string> months,string sortOrder)
    {
        ViewBag.BrandSortParam = String.IsNullOrEmpty(sortOrder) ? "brand_desc" : "";

        ViewBag.DisciplineSortParm = sortOrder == "discipline" ? "discipline_desc" : "discipline";

        ViewBag.MonthSortParm = sortOrder == "month" ? "month_desc" : "month";

        ViewBag.QuantitySortParm = sortOrder == "quantity" ? "quantity_desc" : "quantity";

        ViewBag.ValueSortParm = sortOrder == "value" ? "value_desc" : "value";


        model.ExposureRecords = FilterExposure(brands, disciplines, months);
        model.ExposureRecords = Sort(sortOrder, model.ExposureRecords);
        if (Request.IsAjaxRequest())
            return PartialView("_Table", model);
        return View(model);
    }

1 个答案:

答案 0 :(得分:2)

我的建议是使用客户端库。排序可以在控制器中完成,但根据我的经验,它通常很痛苦。我最近在使用sorttable.js的项目中完成了您想要完成的任务,尽管有多种选择可供选择(请参阅Porschiey上面的评论)。

要使用sortable,请添加&#34; sortable&#34;你的桌子上课。另外,一定要运行&#34;可销售&#34; js使用ajax.beginform ajaxoptions中的OnComplete属性,这样表将在ajax调用后排序。像这样:

@using (Ajax.BeginForm(new AjaxOptions() {HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "exposureRecords", OnComplete="makesortable"}))
{
...
}

function makesortable() {
sorttable.makeSortable(document.getElementById('exposureRecords'));
};