我正在使用 MVC。我想传递我从视图中输入的类别数据并传递给我的 Post/Createcontroller,但它不允许我传递我从下拉列表中选择的 categoryTypeID。
这是错误:
DataBinding:“System.Web.Mvc.SelectListItem”不包含名为“CategoryTypeID”的属性。
这是我的代码:
My CreateController:
//
// POST: /Category/Create
[HttpPost]
public ActionResult Create(Category category)
{
if (ModelState.IsValid)
{
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryTypes = new SelectList(db.CategoryTypes, "CategoryTypeID", "Name", category.CategoryTypeID);
return View(category);
}
My Create View
@model Haykal.Models.Category
<div class="editor-label">
@Html.LabelFor(model => model.CategoryTypeID, "CategoryType")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CategoryTypeID,
new SelectList(ViewBag.CategoryTypes as System.Collections.IEnumerable, "CategoryTypeID", "Name"),
"--select Category Type --", new { id = "categoryType" })
@Html.ValidationMessageFor(model => model.CategoryTypeID)
</div>