tags:

views:

91

answers:

1

Hello

I have a problem with inserting a list of int's to an object. The controller looks like:

 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();
        }
    }

How do I update/insert all the list of CategoryID's for "product.Categories"?

And shall I have something else rather than "int[] CategoryID"?

Thanks in advance /M

+1  A: 

I don't see how your View looks as that is the key point.
Your view should generate HTML similar to this:

<input type="hidden" name="CategoryId[0]" value="123" />
<input type="hidden" name="CategoryId[1]" value="456" />
<input type="hidden" name="CategoryId[2]" value="789" />

Note that the index should not have holes and you will probably have othe type of input instead of hidden.

Dmytrii Nagirniak