我正在尝试创建级联下拉列表,但出现错误。
这包含我在控制器类中尝试做的事情:
public ActionResult Create()
{
ViewBag.Status = new SelectList(db.pjt_Statuses, "pjt_Status_ID", "StatusName");
ViewBag.CategoryID = new SelectList(db.pjt_Categories, "pjt_Category_ID", "CatName");
return View();
}
public ActionResult SubCategory(int id)
{
var SubCategory = from s in db.pjt_SubCategories
where s.CategoryID == id
select s;
return Json(SubCategory.ToList());
}
// POST: /Project/Create
[HttpPost]
public ActionResult Create(pjt_Projects pjt_projects)
{
if (ModelState.IsValid)
{
pjt_projects.CreationDate = DateTime.Now;
db.pjt_Projects.Add(pjt_projects);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Status = new SelectList(db.pjt_Statuses, "pjt_Status_ID", "StatusName", pjt_projects.Status);
ViewBag.CategoryID = new SelectList(db.pjt_Categories, "pjt_Category_ID", "CatName", pjt_projects.CategoryID);
return View(pjt_projects);
}
看法
这显示了我在前端尝试做的事情。我在下面的视图代码中收到粗体线错误。我得到的错误是:
没有
IEnumerable<SelectListItem>
具有 key的 ViewData 类型的项目pjt_SubCat_ID
。
代码:
<div class="span3 offset1">
@Html.LabelFor(model => model.CategoryID, "Category")
@Html.DropDownList("CategoryID", String.Empty)@*, null, new { @onchange = "FetchSubCategories();" })*@
<br />
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="span3 offset1">
<label>Sub Category</label>
@Html.DropDownList("pjt_SubCat_ID", String.Empty)
<br />
@Html.ValidationMessageFor(model => model.SubCategoryID)
</div>
有人可以告诉我这里出了什么问题吗?