Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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# 在Asp.Net核心MVC中搜索_C#_Asp.net Core - Fatal编程技术网

C# 在Asp.Net核心MVC中搜索

C# 在Asp.Net核心MVC中搜索,c#,asp.net-core,C#,Asp.net Core,按类别筛选正确显示的页面。从搜索框中搜索只需在第一页给出结果,它显示所有项目的页数 我的控制器如下: public ActionResult Index(string category, string search, int page = 1) { var viewModel = new ProductsListViewModel(); var query = repository.Products; if (!Str

按类别筛选正确显示的页面。从搜索框中搜索只需在第一页给出结果,它显示所有项目的页数

我的控制器如下:

        public ActionResult Index(string category, string search, int page = 1)
    {
        var viewModel = new ProductsListViewModel();

        var query = repository.Products;


        if (!String.IsNullOrEmpty(category))
        {
            query = query.Where(p => p.Category == category);
            viewModel.CurrentCategory = category;
        }

        if (!String.IsNullOrEmpty(search))
        {
            query = query.Where(p => p.Name.Contains(search) ||
            p.Details.Contains(search);
            viewModel.Search = search;
        }

        viewModel.Products = query
                        .OrderByDescending(p => p.ProductID)
                        .Skip((page - 1) * PageSize)
                        .Take(PageSize).ToList();


        viewModel.PagingInfo = new PagingInfo()
        {
            CurrentPage = page,
            ItemsPerPage = PageSize,
            TotalItems = category == null ?
                        repository.Products.Count() :
                        repository.Products.Where(e =>
                            e.Category == category).Count()

        };

        return View(viewModel);
    }
视图的“我的页面”链接:


我的PaginInfo.cs是:

    public int TotalItems { get; set; }
    public int ItemsPerPage { get; set; }
    public int CurrentPage { get; set; }

    public int TotalPages =>
        (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage);

你可以这样分解它:

public ActionResult List(string category, string search, int page = 1)
{
    var model = new ProductsListViewModel();

    var query = repository.Products;


    if (!String.IsNullOrEmpty(category))
    {
        query = query.Where(p => p.Category == category);
        model.CurrentCategory = category;
    }

    if (!String.IsNullOrEmpty(search))
    {
        query = query.Where(p => p.Description.Contains(search));
        model.Search = search;
    }

    var count = query.Count();

    model.Products = query
                    .OrderByDescending(p => p.ProductID)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize).ToList();

    model.PagingInfo = new PagingInfo()
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = count
                };

    return View(model);
}

.Where(p=>p.Description.Contains(search))
?您需要将查询强制转换为可查询,以便根据parameters@Pawel,如何使用
编写
。其中(p=>p.Description.Contains(search))
。其中(p=>category==null | | p.category==category)
在同一方法中?@RochaCarter07检查我的edit@DawoodAwan我明白了,它工作得很好。问题是页面没有正确显示搜索功能。我会试着给它定路线。