我在向对象插入int列表时遇到问题。控制器看起来像:
public ActionResult Edit(int productID)
{
//ProductEditViewModel pm = new ProductEditViewModel();
Product product = _pr.GetProducts().ByProductID(productID).First();
product.Categories.Load();
//ICollection<Category> allCategories = _cr.GetCategories().ToList();
List<SelectListItem> Categories = (from category in _cr.GetCategories().ToList()
join pc in product.Categories
on category.CategoryID equals pc.CategoryID into j
select
new SelectListItem
{
Selected = j.Any(),
Value = category.CategoryID.ToString(),
Text = category.Categoryname
}).ToList();
ViewData["allCategories"] = Categories;
return View("Edit", new ProductEditViewModel { Product = product });
}
//
// POST: /Products/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Product product, int[] CategoryID)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
如何更新/插入“product.Categories”的所有CategoryID列表?
我还有别的东西而不是“int [] CategoryID”吗?
提前致谢 / M
答案 0 :(得分:1)
我看不到您的View的外观,因为这是关键点 您的视图应生成类似于此的HTML:
<input type="hidden" name="CategoryId[0]" value="123" />
<input type="hidden" name="CategoryId[1]" value="456" />
<input type="hidden" name="CategoryId[2]" value="789" />
请注意,索引不应该有洞,你可能会有其他类型的输入,而不是隐藏。