1

以下のエラーについて教えてください。私のコードの何が問題なのですか。ご協力いただきありがとうございます。

エラー:

キー「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)
4

1 に答える 1

1

create の post アクションでビューバッグに再度データを入力したところ、解決しました。

于 2014-09-18T18:25:34.333 に答える