我正在使用MVC。我想傳遞從視圖中輸入的類別數據並將其傳遞給我的Post/Createcontroller,但它不是讓我通過從我的下拉列表中選擇的categoryTypeID。DataBinding:'System.Web.Mvc.SelectListItem'不包含名稱爲'CategoryTypeID'的屬性
以下是錯誤:
數據綁定:「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>
+ 1。的確,我已經定義了SelectList兩次。你的解釋絕對解決了我的問題。現在,爲什麼錯誤信息實際上並沒有這樣說?我有類似的錯誤:DataBinding:'System.Web.Mvc.SelectListItem'不包含名稱爲'Id'的屬性。錯誤信息是某種誤導,你不覺得嗎? – AuroMetal 2016-01-06 08:46:22