没有具有键 'ddlcontent' 的类型 'IEnumerable<SelectListItem>' 的 ViewData 项

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ddlcontent'

当我尝试访问控制器中的下拉值时,我曾遇到此错误 "There is no ViewData item of type 'IEnumerable' that has the key 'ddlcontent'"。那么现在的问题是,当我使用下面的当前视图代码时,它什么也不显示。请告诉我我在哪里遗漏了它。

我尝试检查的链接是: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'CategoryID'

但到目前为止,其中 none 正在提供帮助。 查看

      @model List<DemoWork.Models.Attendance>
      @using (Html.BeginForm("TakeDailyAttendance", "Trainer", FormMethod.Get))
 {

<table align="center">
    <tr>
        <th>

            @Html.Label("Event name", htmlAttributes: new { @class = "control-label" })

            @Html.DropDownList("dllcontent",new SelectList(ViewData.Model.Select(x=>x.Subject), ViewBag.dllcontent as SelectList), "-- select  --")
        </th>

        <th>
            <div class="col-lg-6">

                @Html.Label("StartDate", "From:", htmlAttributes: new { @class = "control-label" })
                <input id="startdate" name="startdate" type="date" value="" class="form-control">
            </div>

            <div class="col-lg-6">
                @Html.Label("enddate", "To:", htmlAttributes: new { @class = "control-label" })
                <input id="enddate" name="enddate" type="date" value="" class="form-control">
            </div>
        </th>

        <th>

            <div class="col-md-offset-2 col-md-10" style="margin-top:25px;">

                <input type="submit" value="Search" class="btn btn-default" />
            </div>

        </th>
    </tr>
</table>




}

控制器

       [HttpGet]
       public ActionResult TakeDailyAttendance(string ddlcontent, DateTime? startdate, DateTime? enddate)
        {
        var listone = new List<string>();
        var nameqry = from n in db.Attendances
                      select n.Subject;

        listone.AddRange(nameqry.Distinct());

        ViewBag.ddlcontent = new SelectList(listone);

        var tb_teachers = db.Attendances.Where(x => x.StartDate >= startdate && x.EndDate <= enddate && x.Subject.Contains(ddlcontent) && x.Approval == "Going").ToList();

        return View(tb_teachers.ToList());
    }

型号

[Table("Attendance")]
public partial class Attendance
{
    public int AttendanceId { get; set; }

    [StringLength(50)]
    public string TeacherId { get; set; }

    [StringLength(50)]
    public string FirstName { get; set; }

    [StringLength(50)]
    public string LastName { get; set; }

    [StringLength(50)]
    public string IdNumber { get; set; }

    [StringLength(200)]
    public string EmailId { get; set; }

    [StringLength(50)]
    public string PhoneNumber { get; set; }

    [StringLength(200)]
    public string SchoolName { get; set; }

    [StringLength(50)]
    public string District { get; set; }

    [StringLength(50)]
    public string Province { get; set; }

    public DateTime? Date { get; set; }

    public int? EventID { get; set; }

    [StringLength(100)]
    public string Subject { get; set; }

    [StringLength(300)]
    public string Description { get; set; }

    [StringLength(200)]
    public string Location { get; set; }

    [Column(TypeName = "date")]
    public DateTime? StartDate { get; set; }

    [Column(TypeName = "date")]
    public DateTime? EndDate { get; set; }

    [StringLength(100)]
    public string CourseId { get; set; }

    [StringLength(50)]
    public string AttendaceStatus { get; set; }


    [StringLength(50)]
    public string Approval { get; set; }

    [StringLength(50)]
    public string Pin { get; set; }
}

来自asp-net-mvc-dropdown-list-from-selectlist

You are missing setting what field is the Text and Value in the SelectList itself. That is why it does a .ToString() on each object in the list. You could think that given it is a list of SelectListItem it should be smart enough to detect this... but it is not.

因此在您的情况下,您需要将 List<String> 转换为 List<SelectListItem>,如下所示:

ViewBag.ddlcontent = new SelectList(listone.Select(i=> new SelectListItem()
                                            {
                                                Text = i,
                                                Value = i
                                            }).ToList(),"Value" , "Text");