gpt4 book ai didi

linq - 如何在MVC中使用两个表在 Entity Framework 中进行分页

转载 作者:行者123 更新时间:2023-12-02 04:53:21 26 4
gpt4 key购买 nike

我想用两个表实现分页,我试图在谷歌中找到但不是两个表
以下代码引用http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

http://dotnetmentors.com/mvc/paging-and-sorting-in-asp-net-mvc-and-entity-framework-application.aspx

http://www.c-sharpcorner.com/UploadFile/4b0136/perform-paging-searching-sorting-in-Asp-Net-mvc-5/

我当前的代码如下

public class PagedProductModel
{
public int TotalRows { get; set; }
public IEnumerable<Product> Product { get; set; }
public int PageSize { get; set; }
public List< ProductVariation> ProductVariantlist { set; get; }
}

public ActionResult ProductList(int stockquantity = -1, string searchWord = null, int page = 1, string sort = "ProductID", string sortDir = "ASC")
{
const int pageSize = 10;
var totalRows = CountProduct();
bool Dir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? true : false;

var product = GetProductPage(page, pageSize, sort, Dir, searchWord, stockquantity);
var data = new PagedProductModel()
{
TotalRows = totalRows,
PageSize = pageSize,
Product = product
};


return View(data);
}

public IEnumerable<Product> GetProductPage(int pageNumber, int pageSize, string sort, bool Dir, string searchText, int stockquantity)
{
if (pageNumber < 1)
pageNumber = 1;
var query = (IQueryable<Product>)_objProductContext.ProductEntries.OrderByWithDirection(c => c.ProductID, Dir);
query = filterproduct(searchText, stockquantity, query);
return query
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
}

private static IQueryable<Product> filterproduct(string searchText, int stockquantity, IQueryable<Product> query)
{
if (!string.IsNullOrWhiteSpace(searchText))
{

if (stockquantity == -1)
{
query = query.Where(p => p.ProductName.Contains(searchText));
}
else
{

}

}
if (stockquantity != -1)
{

if (stockquantity == -1)
{
query = query.Where(p => p.ProductName.Contains(searchText));
}
else
{
ProductVariation objProductVariation = new ProductVariation();

ProductVariationContext _contextProductVariation = new ProductVariationContext();

query = query.Where(x => x.ProductID == objProductVariation.ProductID && objProductVariation.Quantity == stockquantity);
}

}



return query;
}

一种产品有多种产品变化

请建议我

谢谢

最佳答案

我完成多个可分页网格的一种方法是利用 KendoGrid

使用返回 JSON 的 AJAX 调用将每个连接起来,网格将为您处理分页。

关于linq - 如何在MVC中使用两个表在 Entity Framework 中进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25735824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com