【问题标题】:There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'StudentID'没有具有键 'StudentID' 的 'IEnumerable<SelectListItem>' 类型的 ViewData 项
【发布时间】:2017-02-06 21:08:47
【问题描述】:

我知道这类错误有很多问题,我尝试对我的问题实施这些修复,但似乎没有任何效果。

我相信 ASP.Net 教程中 EnrollmentController 类中的 Create() 方法的限制会给出此错误消息。我有一个限制,如果学生之前已经注册过一门课程,他/她不能再次注册。当我尝试测试限制时,它给了我 ViewData 错误消息。

    public ActionResult Create([Bind(Include = "EnrollmentID,CourseID,StudentID")]Enrollment enrollcourse)
            {
                if (ModelState.IsValid)
                {
                    var result = db.Enrollments.Count(u => u.StudentID == enrollcourse.StudentID && u.CourseID == enrollcourse.CourseID) == 0;
                    if (result)
                    {
                        TempData["success"] = "Course Enrolled";
                        db.Enrollments.Add(enrollcourse);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        TempData["Already"] = "Student Has Already Enrolled This Course";
ViewBag.StudentId = new SelectList(db.People, "ID", "FullName", enrollcourse.StudentID);
                ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Title", enrollcourse.CourseID);
                        return View("Create");
                    }
                }

                return View(enrollcourse);
            }

错误消息告诉我在我的 Views/Enrollment/Create.cshtml

中修复这行代码
@Html.DropDownList("StudentID", null, htmlAttributes: new { @class = "form-control" })

我是 .Net 和 C# 的新手,我正在尽我最大的努力通过,但它变得非常混乱和复杂。 “没有 'IEnumerable... 类型的 ViewData 项”是什么意思?

【问题讨论】:

  • 因为您没有StudentID 的密钥,所以您有StudentId 的密钥。使用@Html.DropDownList("StudentId",...
  • 我尝试更改它,但同样类型的错误。
  • ModelState 键不区分大小写:StudentID == StudentId == StUdEnTiD。没关系。
  • 因为ModelState 无效并且您在return View(enrollcourse); 之前没有填充ViewBag.StudentId

标签: c# asp.net-mvc


【解决方案1】:

else 块中,您将返回相同的视图 - 没有设置 ViewBag。

将这两个 ViewBag 分配移动到 if() 块之前。

或者更确切地说:

public ActionResult Create([Bind(Include = "EnrollmentID,CourseID,StudentID")]Enrollment enrollcourse)
{
    if (ModelState.IsValid)
    {
        if (!IsStudentEnrolled(enrollcourse))
        {
            db.Enrollments.Add(enrollcourse);
            db.SaveChanges();
            TempData["success"] = "Course Enrolled";
            return RedirectToAction("Index");
        }
        else
        {
            TempData["Already"] = "Student Has Already Enrolled This Course";
        }
    }

    ViewBag.StudentId = new SelectList(db.People, "ID", "FullName", enrollcourse.StudentID);
    ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Title", enrollcourse.CourseID);

    return View(enrollcourse);
}

private bool IsStudentEnrolled(Enrollment enrollcourse)
{
    return db.Enrollments.Count(u => u.StudentID == enrollcourse.StudentID && u.CourseID == enrollcourse.CourseID) == 0;        
}

由于您要返回到无效模型状态或已注册课程的“创建”视图,您可以直接进入最后一个return View(enrollcourse)

如果您想在学生已注册该课程时清除视图,则else 应重置视图模型:

else
{
    enrollcourse = null;
    ModelState.Clear();

    TempData["Already"] = "Student Has Already Enrolled This Course";
}

如果您在更多地方需要,也可以perform this validation in an attribute

【讨论】:

  • 如果我在 else 循环中移动 2 个 ViewBag 语句,则不会显示错误消息,但不会显示 TempData["Already"] = "Student Has Already Enrolled This Course"; 消息。
  • 那么您不会在视图上呈现该字符串。
  • 我试过return View("Already", enrollcourse);,但它给了我服务器错误消息。
  • 如果您遇到错误,请研究它。该语法不适用于在视图上呈现 TempData 值,它指示 MVC 呈现名为“Already”的视图,该视图不存在,这是服务器错误告诉您的。如果要呈现该字符串,则需要在视图文件中使用 @Tempdata["Already"]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-26
  • 2020-06-28
  • 2020-08-20
  • 2011-02-20
  • 2015-03-11
相关资源
最近更新 更多