ASP.net 使用 link 对多个复选框列表进行 MVC 过滤

ASP.net MVC Filter on multiple checkboxlist using link

我正在开发一个 Asp MVC 应用程序,其中包含我想用复选框列表过滤的数据网格。我希望能够从一个或多个选择中过滤我的数据。目前它仅适用于一个选择。我使用 link 表达式来过滤我的数据。我想在 sql 中有以下等价物:

select * from mission_supportmission where decision in (item1, item2..)

我的观点是这样的:

<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);
            foreach (var item in selecteddecision)
            {                     
                    model.OnePageOfMissions = db.missions_supportmission
                                .Where(a => a.decision.Contains(item))
                                .OrderBy(a => a.id)
                                .Select(s => s).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; }

    public bool IsChecked;
  }
}
//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.missionID equals l.missionID
                              where selecteddecision.Contains(l.decisionID)
                              select m)
                              .OrderBy(a => a.id)
                              .ToPagedList(pageNumber, 10);
}

没有关于对象的任何数据结构信息,这是我最好的解决方法。