如何更改下拉内容

我正在尝试在 ASP.NET MVC 中执行下拉筛选内容。我希望下拉列表显示三种毕业状态(已毕业、通过、未通过),当我选择一个时,它只会显示所选状态的学生!

控制器:


ViewBag.GraduationStatus = new SelectList(db.Graduated_Students, "Graduated Status");


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

return View(graduates.ToList());

看法:


@using (Html.BeginForm())

{

    <table>

        <tr>

            <th>

                @Html.DropDownList("GraduationStatus", null, htmlAttributes: new { @class = "form-control" })

            </th>

            <th>

                <input type="submit" value="Filter by graduation Status" />

            </th>

        </tr>

    </table>

}

模型 :


namespace CodeboxxSchoolPortal

{

    using System;

    using System.ComponentModel.DataAnnotations;

    using System.Collections.Generic;


    public partial class Graduated_Students

    {

        public int ID { get; set; }

        public int CohortId { get; set; }

        public string GraduationStatus { get; set; }

        public string Name { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string ContractStatus { get; set; }

        public string DepositStatus { get; set; }

        public Nullable<int> PartnerId { get; set; }

        public string PartnerName { get; set; }



            public List<SelectListItem> GraduationStatusList = new List<SelectListItem>

            {

                 new SelectListItem { Value="Graduated", Text="Graduated" },

                 new SelectListItem { Value ="Pass", Text="Pass" },

                 new SelectListItem { Value ="Failed", Text="Failed" }

            };


        public class SelectListItem

        {

            public string Value { get; set; }

            public string Text { get; set; }

        }


    }

}


千万里不及你
浏览 118回答 1
1回答

胡子哥哥

这是我认为你应该做的,如果您Action在控制器中是GetStudents,并且您的下拉列表的值为字符串(毕业状态),请将其传递给过滤器按钮上的控制器单击&nbsp;[HttpPost]&nbsp;public ActionResult Documents(string? graduationStatus)&nbsp;{&nbsp; &nbsp; &nbsp;var graduates = db.Graduated_Students.Where(student => student.GraduationStatus == graduationStatus);&nbsp; &nbsp; &nbsp;return View(graduates.ToList());&nbsp;}请注意,必须与数据库中的string? graduationStatus类型相同student.GraduationStatus还要确保Html.BeginForm()对控制器有正确的操作@Html.BeginForm("GetStudents", "Controller", FormMethod.Post, new { @class = "my_form"})编辑:将下面的行更改为@Html.DropDownList("GraduationStatus", new SelectList(ViewBag.GraduationStatus, "Value Here", "Text Here", null), "- Select -", new { @class = "form-control"})
打开App,查看更多内容
随时随地看视频慕课网APP