我想創建一個級聯下拉列表,並且出現錯誤。級聯下拉列表
這包含了什麼,我試圖在我的控制器類的事:
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);
}
查看
這說明什麼,我試圖在前端做。我在下面的視圖代碼中看到粗體行出現錯誤。我得到的錯誤是:
有是有鑰匙
pjt_SubCat_ID
IEnumerable<SelectListItem>
類型的無ViewData的項目。
代碼:
<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>
有人能告訴我是怎麼回事錯在這裏?
哪條粗線?這一個:@ Html.DropDownList(「pjt_SubCat_ID」,String.Empty)? – JTMon
O它沒有變得大膽,是它的那一行,對不起@JTMon – PatrickMelia