Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 插入int'的列表;s_C#_Asp.net Mvc - Fatal编程技术网

C# 插入int'的列表;s

C# 插入int'的列表;s,c#,asp.net-mvc,C#,Asp.net Mvc,我在向对象插入int的列表时遇到问题。控制器看起来像: public ActionResult Edit(int productID) { //ProductEditViewModel pm = new ProductEditViewModel(); Product product = _pr.GetProducts().ByProductID(productID).First(); product.Categories.Load();

我在向对象插入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();
        }
    }
公共操作结果编辑(int productID) { //ProductEditViewModel pm=新的ProductEditViewModel(); Product Product=\u pr.GetProducts().ByProductID(productID).First(); product.Categories.Load(); //ICollection allCategories=\u cr.GetCategories().ToList(); 列表类别=(来自_cr.GetCategories().ToList()中的类别) 加入pc的产品类别 在category.CategoryID上等于pc.CategoryID到j 选择 新建SelectListItem { 所选=j.Any(), Value=category.CategoryID.ToString(), Text=category.Categoryname }).ToList(); ViewData[“所有类别”]=类别; 返回视图(“编辑”,新ProductEditViewModel{Product=Product}); } // //发布:/Products/Edit/5 [接受动词(HttpVerbs.Post)] 公共行动结果编辑(产品,int[]类别ID) { 尝试 { //TODO:在此处添加更新逻辑 返回操作(“索引”); } 抓住 { 返回视图(); } } 如何更新/插入“产品类别”的所有类别ID列表

我要别的东西而不是“int[]CategoryID”吗

提前谢谢
/我看不出你的观点如何,因为这是关键点。
您的视图应生成类似以下内容的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" />


请注意,索引不应该有洞,您可能会有其他类型的输入,而不是隐藏的。

我看不出您的视图是什么样子,因为这是关键点。
您的视图应生成类似以下内容的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" />


请注意,索引不应该有漏洞,您可能会有其他类型的输入,而不是隐藏的。

您在那里使用的是LINQ to SQL吗?在我看来,您的问题与您正在使用的数据访问技术的关系比与ASP.NET MVC的关系更大,因此我建议您在这个主题上进行一些扩展,或许可以重新标记您的问题?您在那里使用的是LINQ到SQL吗?在我看来,您的问题与您正在使用的数据访问技术的关系比与ASP.NET MVC的关系更大,因此我建议您在这个主题上进行一些扩展,或许可以重新标记您的问题?