我正在使用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>
Run Code Online (Sandbox Code Playgroud)