ASP.NET MVC pagedlist松动的复选框列表筛选器

时间:2018-11-07 15:46:15

标签: asp.net-mvc filter checkboxlist pagedlist

我正在开发一个Asp MVC应用程序,该应用程序包含可以使用复选框列表过滤的数据网格。我正在使用PagedList来显示数据。我的过滤器在第一页上运行良好,但是如果单击第二页,则过滤器被取消。我没有使用dropdownlist进行过滤的问题(以下示例中没有)。

我的视图如下:

<div class="wrapper">
<nav id="sidebar">
    @using (Html.BeginForm("Index", "Missions", FormMethod.Get))
    {
        @Html.EditorFor(x => x.Decision)
    }
</nav>
<div id="content" class="container">
    <table class="table table-bordered">
        <tr>
            <th class="col-md-2">
                Decision
            </th>

        </tr>
        @foreach (var item in Model.OnePageOfMissions)
        {
        <tr>
            <td class="col-md-1">
                @Html.DisplayFor(modelItem => item.decision)
            </td>
        </tr>
        }
        @Html.PagedListPager((IPagedList)Model.OnePageOfMissions, page => Url.Action("Index", new { page, Decision = Model.Decision}))
    </table>
</div>

我创建了一个模板来显示复选框列表,如下所示:

<div class="form-check row">
@Html.HiddenFor(x => x.ID)
@Html.CheckBoxFor(x => x.IsChecked, htmlAttributes: new { onchange = "form.submit();", @class = "form-check-input col-md-2" })
@Html.LabelFor(x => x.Display, Model.Display, htmlAttributes: new { @class = "form-check-label col-md-8" })

我的控制器如下:

 public ActionResult Index(int? page, List<CheckBoxListItem> Decision)
    {
        IndexViewModel model = new IndexViewModel();
        //Display Missions
        model.missionsList = db.missions_supportmission.ToList();
        //Retrieve parameters    
        model.Decision = Decision;
        //PagedList
        var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
        var onePageOfMissions = model.missionsList.ToPagedList(pageNumber, 10); // will only contain 10 products max because of the pageSize(equel to 10)
        model.OnePageOfMissions = onePageOfMissions;
         //Filter
        if (model.Decision != null)
        {
            var selecteddecision = model.Decision.Where(x => x.IsChecked).Select(x => x.ID);
            model.OnePageOfMissions = (from m in db.missions_supportmission
                                       join l in db.list_decision
                                       on m.decision equals l.decision_id
                                       where selecteddecision.Contains(l.decision_id)
                                       select m)
                                      .OrderBy(a => a.id)
                                      .ToPagedList(pageNumber, 10);
        }
            }
        }
        //Display CheckBox
        //Checkboxlist (!important => mettre ce bloc de code après la requête link qui permet de filtrer sur les checkbox)
        var allDecisions = db.list_decision.ToList();//returns List<list_decision>
        var checkBoxListItems = new List<CheckBoxListItem>(); //nouvelle instance de la classe checkboxlist
        model.Decision = checkBoxListItems;
        foreach (var decison in allDecisions)
        {//On assigne les valeurs "id", "display" et "is checked" à la variable checkboxlistitem
            checkBoxListItems.Add(new CheckBoxListItem()
            {
                ID = decison.decision_id,
                Display = decison.name_en,
                IsChecked = false //On the add view, no decision are selected by default
            });
        }

        return View(model);
    }

我还有一个ViewModel:

namespace MissionsDF.Models
{
public class IndexViewModel
{

    public IEnumerable<missions_supportmission> missionsList { get; set; }
    public List<CheckBoxListItem> Decision { get; set; }
    public IndexViewModel()
    {
        this.Decision = new List<CheckBoxListItem>();
    }
    public IPagedList<missions_supportmission> OnePageOfMissions { get; set; }

  }
}

0 个答案:

没有答案