首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有具有'ddlcontent‘键的'IEnumerable<SelectListItem>’类型的IEnumerable<SelectListItem>项。

没有具有'ddlcontent‘键的'IEnumerable<SelectListItem>’类型的IEnumerable<SelectListItem>项。
EN

Stack Overflow用户
提问于 2020-03-17 12:59:42
回答 1查看 1.6K关注 0票数 0

当我试图访问控制器中的下拉值时,我会得到这个错误:“没有‘ViewData’类型的'IEnumerable‘项,它的键'ddlcontent'”。现在的问题是,当我使用下面的当前视图代码时,它不会显示任何内容。请告诉我我错过了哪里。

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

The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable'

但到目前为止,他们都没有提供帮助。视图

代码语言:javascript
复制
      @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>




}

控制器

代码语言:javascript
复制
       [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());
    }

模型

代码语言:javascript
复制
[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; }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-17 14:19:52

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

--您缺少设置文本在SelectList本身中的字段。这就是为什么它对列表中的每个对象执行.ToString()的原因。你可能会认为,考虑到这是一个SelectListItem列表,它应该足够聪明地检测到这一点.但事实并非如此。

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

代码语言:javascript
复制
ViewBag.ddlcontent = new SelectList(listone.Select(i=> new SelectListItem()
                                            {
                                                Text = i,
                                                Value = i
                                            }).ToList(),"Value" , "Text");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60722951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档