Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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# 基于多个复选框筛选产品_C#_Asp.net_Entity Framework_Model View Controller_Asp.net Ajax - Fatal编程技术网

C# 基于多个复选框筛选产品

C# 基于多个复选框筛选产品,c#,asp.net,entity-framework,model-view-controller,asp.net-ajax,C#,Asp.net,Entity Framework,Model View Controller,Asp.net Ajax,我在电子商务网站工作。我想根据多个复选框筛选产品。我使用ajax。它将复选框的ID发送给控制器。但“选定”计数为零,与图片中的一样。这个代码有什么问题 阿贾克斯: 控制器: [HttpPost] public JsonResult GetProd(string Id) { var ids = new List<int>(); IQueryable<Product> prods = null; if

我在电子商务网站工作。我想根据多个复选框筛选产品。我使用ajax。它将复选框的ID发送给控制器。但“选定”计数为零,与图片中的一样。这个代码有什么问题

阿贾克斯:

控制器:

  [HttpPost]
    public JsonResult GetProd(string Id)
    {
            var ids = new List<int>();
        IQueryable<Product> prods = null;
        if (!string.IsNullOrEmpty(Id))
        {
            for (int i = 0; i < Id.Split(',').Length; i++)
            {
                ids.Add(Convert.ToInt32(Id.Split(',')[i]));
            }
           prods =_context.Products.Where(t => ids.Contains(t.id));
        }
        else
        {
           prods = _context.Products.Take(5);
        }
        var selected = (from v in prods
                     select new
                     {
                         v.ProdName,
                         v.Price,
                         v.Description
                     }).ToList();

        return Json(selected, JsonRequestBehavior.AllowGet);

    }

尝试在如下控制台中执行此操作:


您确定$this.val返回产品id吗?这意味着值1,2是数据库中现有的ID非常感谢。İ应该写CategoryId而不是id。现在计数不是零。好了。代码正在运行,但可以重构一点:PIt只是让您在控制台中看到此代码正常工作。您使用的是什么版本的Net Framework?它应该可以正常工作。我认为_context.Products是一个空列表,你能确保列表不是空的吗?谢谢你的回答。问题解决了。问题是它必须是t=>ids.Containst.CategoryId
  [HttpPost]
    public JsonResult GetProd(string Id)
    {
            var ids = new List<int>();
        IQueryable<Product> prods = null;
        if (!string.IsNullOrEmpty(Id))
        {
            for (int i = 0; i < Id.Split(',').Length; i++)
            {
                ids.Add(Convert.ToInt32(Id.Split(',')[i]));
            }
           prods =_context.Products.Where(t => ids.Contains(t.id));
        }
        else
        {
           prods = _context.Products.Take(5);
        }
        var selected = (from v in prods
                     select new
                     {
                         v.ProdName,
                         v.Price,
                         v.Description
                     }).ToList();

        return Json(selected, JsonRequestBehavior.AllowGet);

    }
using System;
using System.Collections.Generic;
using System.Linq;


public class Product
{
   public int Id { get; set; }

   public string ProdName { get; set; }

   public decimal? Price { get; set; }

   public string Description { get; set; }
}

public class Program
{
 public static void Main(string[] args)
 {
    IQueryable<Product> products = (new List<Product> {
            new Product
            {
                Id = 1,
            },
            new Product
            {
                Id = 2,
            }
        }).AsQueryable<Product>();

    var Id = "1,2";
    var ids = new List<int>();
    IQueryable<Product> prods = null;
    if (!string.IsNullOrEmpty(Id))
    {
        for (int i = 0; i < Id.Split(',').Length; i++)
        {
            ids.Add(Convert.ToInt32(Id.Split(',')[i]));
        }
        prods = products.Where(t => ids.Contains(t.Id));
    }
    else
    {
        prods = products.Take(5);
    }
    var selected = (from v in prods
                    select new
                    {
                        v.ProdName,
                        v.Price,
                        v.Description
                    }).ToList();


    Console.WriteLine(selected.Count);

  }
}