【问题标题】:How can I change the dropdown content如何更改下拉内容
【发布时间】:2019-06-27 18:02:12
【问题描述】:

我正在尝试在 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; }
        }

    }
}

【问题讨论】:

  • 请看一下这个演示,如果它满足您的需求,请告诉我:dotnetfiddle.net/OBg7Rp
  • 我的例子对你有帮助吗?
  • 感谢您的帮助,抱歉迟到了!我现在正试图让它工作,但它仍然不会因为视图中的这个错误:'IEnumerable'不包含'GraduationStatus'的定义并且没有可访问的扩展方法'GraduationStatus'接受一个可以找到“IEnumerable”类型的第一个参数(您是否缺少 using 指令或程序集引用?)
  • 发生这种情况是因为您发送到视图的模型没有正确绑定到 GraduationStatus。如果您将看到我的示例,模型值将绑定到这些值,然后使用 @DropDownListFor 在视图上重新渲染它们。基本上我的观点是你必须在你的控制器中绑定你的 SelectList 值并正确绑定它们。
  • 我已经编辑了我的问题,以便您可以看到我的模型,感谢您的帮助!!我还是卡住了……

标签: c# asp.net-mvc razor model-view-controller


【解决方案1】:

这是我认为你应该做的,

如果您在控制器中的ActionGetStudents,并且您的下拉列表中的值是字符串(毕业状态),请在点击过滤器按钮时将其传递给控制器​​

 [HttpPost]
 public ActionResult Documents(string? graduationStatus)
 {
     var graduates = db.Graduated_Students.Where(student => student.GraduationStatus == graduationStatus);
     return View(graduates.ToList());
 }

请注意,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"})

【讨论】:

  • 我应该把这行代码放在哪里? @Html.BeginForm("GetStudents", "Controller", FormMethod.Post, new { @class= "my_form"}) 感谢您的帮助!!
  • 在你的视图里你有@using(Html.BeginForm())
  • 这是我替换行的地方,但现在在我的索引中,下拉过滤器中没有显示任何内容...感谢您的帮助!
  • 很确定我的问题出在这一行... @Html.DropDownList("GraduationStatus", null, htmlAttributes: new { @class= "form-control" })
猜你喜欢
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2016-04-01
相关资源
最近更新 更多