我有一个注册表单使用一个名为registerViewModel的ViewModel。我正在尝试添加一个允许用户选择成绩的下拉列表。等级数据应来自名为“GradeMatrix”的模型
在我的帐户控制器中,我有以下内容 -
[HttpPost]
public ActionResult Register(RegisterViewModel viewModel)
{
PopulateGradeDropDownList(viewModel.GradeID);
我也有 -
private void PopulateGradeDropDownList(object selectedGrade = null)
{
var gradeQuery = from d in db.GradeMatrix
orderby d.GradeName
select d;
ViewBag.GradeID = new SelectList(gradeQuery, "GradeID", "GradeName", selectedGrade);
}
在我看来我有 -
@ Html.DropDownList(“GradeID”,string.Empty)
当我点击我的注册链接时,我收到以下错误 -
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'GradeID'.
我有什么遗失的吗?
答案 0 :(得分:0)
视图代码应为:
@Html.DropDownList(string.Empty, (SelectList)ViewBag.GradeID)