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