没有类型&#39; IEnumerable <selectlistitem>&#39;的ViewData项目。有键&#39; ****&#39;。</selectlistitem>

时间:2014-10-08 13:56:28

标签: asp.net-mvc-4

我试图在学生索引页面上输入学生注册的下拉列表。

学生管理员:

public ActionResult Index(string studentEnrollment, string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var students = studentService.GetAll();

            //var students = from s in studentService.GetAll()
            //               select s;

            var studentViewModel = Mapper.Map<IEnumerable<Student>, IEnumerable<StudentViewModel>>(students);

            if (!String.IsNullOrEmpty(searchString))
            {
                students = students.Where(s => s.LastName.ToUpper().Contains(searchString.ToUpper())
                                       || s.FirstMidName.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    students = students.OrderByDescending(s => s.LastName);
                    break;
                case "Date":
                    students = students.OrderBy(s => s.EnrollmentDate);
                    break;
                case "date_desc":
                    students = students.OrderByDescending(s => s.EnrollmentDate);
                    break;
                default:
                    students = students.OrderBy(s => s.LastName);
                    break;
            }

            List<Course> courses = new List<Course>();

            SelectList studentEnrollment = new SelectList(courses, "CourseID", "Title");

            ViewBag.Courses = studentEnrollment;

            //if (!string.IsNullOrEmpty(studentEnrollment))
            //    students = students.Where(m => m.Course.Title == studentEnrollment);

            int pageSize = 3;
            int pageNumber = (page ?? 1);

           return View(students.ToPagedList(pageNumber, pageSize));
        }

查看:

    @using (Html.BeginForm("Index", "Student", FormMethod.Get))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <p>
        Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)

        Enrollment: @Html.DropDownList("Course", (IEnumerable<SelectListItem>)ViewBag.Courses, "Select Course")

        <input type="submit" value="Filter" />
    </p>
}

行错误: 注册:@ Html.DropDownList(“课程”,(IEnumerable)ViewBag.Courses,“选择课程”)

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我已经看到很多问题,人们在下拉列表中遇到问题,这是因为查看包。我所看到的一切都说你应该尽量避免使用ViewBag进行下拉。我建议的是将列表放在viewmodel中。

Public List<SelectListItem> CourseList { get; set; }

我没有在您的代码中看到您填写课程列表的位置。在控制器上而不是设置viewbag设置此参数。像

这样的东西
foreach(var temp in CourseList){ //Course list being pulled from the database or wherever that data exists
    model.CourseList.Add(new SelectListItem{ Text = temp.Name, Value = temp.Value });
}

然后在您的视图中,您的下拉列表将更改为

@Html.DropDownList("Course", Model.CourseList, "Select Course")