1
我使用MS Northwind數據庫,並使用實體框架。 我想創建新產品,並使用dropdownList從類別表中加載CategoryName。發佈操作在asp.net中創建MVC
public ActionResult Create()
{
var categories = from c in _en.Categories select c;
ViewData["CategoryID"] = new SelectList(categories, "CategoryID", "CategoryName");
return View();
}
<p>
<label for="ds">CategoryID:</label>
<%=Html.DropDownList("CategoryID", (SelectList)ViewData["CategoryID"])%>
</p>
問題:如何從dropdownList保存數據?
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ProductID")] Products productToCreate)
{
if (!ModelState.IsValid)
return View();
var c = _en.Categories.FirstOrDefault(z => z.CategoryID == ??? XXXX ???);
productToCreate.Categories = c;
_en.AddToProducts(productToCreate);
_en.SaveChanges();
return RedirectToAction("Index");
}
如何從dropdownList中獲取CategoryID?
謝謝。 我是mvc新手。我明白,這很棒。 – loviji 2009-11-17 13:24:20