以下のエラーについて教えてください。私のコードの何が問題なのですか。ご協力いただきありがとうございます。
エラー:
キー「CategoryId」を持つ ViewData 項目は「System.Int32」型ですが、「IEnumerable」型である必要があります。
コントローラ
public ActionResult ProductCreate()
{
ViewBag.Suppliers = db.Suppliers.Where(x => x.IsActive).ToList();
ViewBag.Categories = new SelectList(db.Categories.Where(x => x.IsActive).ToList(), "Id", "Name").ToList();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ProductCreate(Product product, int[] suppliers)
{
if (ModelState.IsValid)
{
foreach (int SupplierId in suppliers)
{
product.Suppliers.Add(db.Suppliers.Find(SupplierId));
}
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("ProductIndex");
}
return View(product);
見る:
@(Html.DropDownListFor(x => x.CategoryId, (IEnumerable<SelectListItem>)ViewBag.Categories))
<br />
@Html.ValidationMessageFor(x => x.CategoryId)