带有db表格列表的多个页面

时间:2018-01-28 13:25:24

标签: asp.net-mvc

我在db上的表中有一个产品列表。我用这段代码把它们放在一个页面上:

public ActionResult Index() {
    var products = productsRepository.GetAll();
    IEnumerable<Models.Product> productsView = products.Select(p => new Models.Product() {
        Id = p.Id,
        Category = p.Category.Name,
        Brand = p.Brand,
        Name = p.Name,
        Type = p.Type,
        IsActive = p.IsActive,
        SellPrice = p.SellPrice,
        AddedDate = p.AddedDate
    });
    return View(productsView);
}

我该如何得到&#39; n&#39;每页产品有多个页面? TY

2 个答案:

答案 0 :(得分:0)

解决它。我改为一个ienumerable,我在重定向页面上有一个错误,但现在一切正常。感谢你 编辑: 在一开始我的分页指数看起来像这样

    public ActionResult Index(int page = 0)
    {

        var products = productsRepository.GetAll();



        const int PageSize = 5; // you can always do something more elegant to set this

        var count = productsView.Count();

        var data = products.OrderBy(o=>o.Brand).Skip(page * PageSize).Take(PageSize).ToList();

        this.ViewBag.MaxPage = (count / PageSize) - (count % PageSize == 0 ? 1 : 0);

        this.ViewBag.Page = page;

        return this.View(data);
    }

我的观点是期待我可以枚举的类型,所以在我添加之后一切都很好。

IEnumerable<Models.Product> productsView = products.Select(p => new Models.Product()
        {
            Id = p.Id,
            Category = p.Category.Name,
            Brand = p.Brand,
            Name = p.Name,
            Type = p.Type,
            IsActive = p.IsActive,
            SellPrice = p.SellPrice,
            AddedDate = p.AddedDate

        });

我是一个乞丐,所以我在这些事情上苦苦挣扎

答案 1 :(得分:0)

您使用BarHelper.Domain.Product和另一个BarHelper.Areas.Admin.Models.Product在一个地方使用不同的类型。因此,请确保您传递相同的类型。