当我试图访问控制器中的下拉值时,我会得到这个错误:“没有‘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'
但到目前为止,他们都没有提供帮助。视图
@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; }
}发布于 2020-03-17 14:19:52
来自asp-net-mvc-dropdown-list-from-selectlist
--您缺少设置文本和值在SelectList本身中的字段。这就是为什么它对列表中的每个对象执行
.ToString()的原因。你可能会认为,考虑到这是一个SelectListItem列表,它应该足够聪明地检测到这一点.但事实并非如此。
因此,在您的示例中,需要将List<String>转换为List<SelectListItem>,如下所示:
ViewBag.ddlcontent = new SelectList(listone.Select(i=> new SelectListItem()
{
Text = i,
Value = i
}).ToList(),"Value" , "Text");https://stackoverflow.com/questions/60722951
复制相似问题