2009-08-18 74 views
1

我有一个int列表插入一个对象的问题。控制器看起来像: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

那是你正在使用的LINQ to SQL吗?在我看来,你的问题与你使用的数据访问技术相比,更多地与ASP.NET MVC相关,所以我建议你在这个主题上扩展一下,也许重申你的问题? – 2009-08-18 08:01:34

回答

1

我不明白你怎么查看看起来这是关键点。
您的视图应该生成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" /> 

注意的是,指数不应有漏洞,你将可能有行吟诗人类型的输入,而不是隐藏