温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Filter button won't reload and filter information
c# razor model-view-controller

c# - 过滤器按钮不会重新加载和过滤信息

发布于 2020-03-27 11:38:34

您好,我正在尝试使用下拉式筛选器按索引页面中的毕业状态进行筛选,但是单击“处理”按钮时,页面将无法筛选...

我在这里做错了什么?

索引页显示过滤器按钮

查看:

@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = 
     "FilterForm" }))
                        {
                            <table>
                                <tr>
                                    <th>


        @Html.DropDownList("GraduationStatus", 
         null, htmlAttributes: new { @class = "form-control" })
                                        </th>
                                        <th>
                                            <button type="submit" class="btn btn- 
          primary btn-lg"> Process !</button>
                                        </th>
                                    </tr>
                                </table>
                            }

控制器:

 public ActionResult Index(string graduationStatus)
        {
            ViewBag.GraduationStatus = new SelectList(db.Graduated_Students.Select(m => m.GraduationStatus).Distinct().ToList());

            var graduates = db.Graduated_Students.Where(student => student.GraduationStatus != null);

            return View(graduates.ToList());
        }

查看更多

查看更多

提问者
Martin
被浏览
83
Muhammad Ali 2019-07-04 00:11

因为您不需要根据初始页面加载进行过滤。初始加载时gradientStatus将为null,因此添加一个条件以检查gradientStatus是否具有某些值。像这样

   public ActionResult Index(string graduationStatus)
    {
      var graduates = db.Graduated_Students.Where(student => student.GraduationStatus != null);
        ViewBag.GraduationStatus = new SelectList(db.Graduated_Students.Select(m => m.GraduationStatus).Distinct().ToList());
         if(!string.IsNullOrEmpty(graduationStatus))
         {
             graduates = graduates .Where(student => student.GraduationStatus == graduationStatus);
         }


        return View(graduates.ToList());
    }