数据库数据分页显示,只能显示首页,后面的点击没反应

2019-03-16 16:06发布

我怀疑是点击事件函数没写对,但我不知道怎么去修改

//分页数字显示:
<div id="pagebar">
    @for (var i = 0; i < ViewBag.PageCount; i++)
    {
        if (i == ViewBag.PageIndex)
        {
            <span class="currentpagenumber">@(i + 1)</span>
        }
        else
        {
            <a class="pagenumber" href="javascript:;">@(i + 1)</a>
        }
    }
</div>

//对应的点击事件:
@section scripts {
    <script>
        function submitForm(pagenumber) {
            pagenumber = parseInt(pagenumber, 10);
            $('#PageIndex').val(pagenumber - 1);
            $('#searchForm').submit();
        }

        $(function () {

            $('#searchButton').click(function () {
                submitForm($('#pagebar .currentpagenumber').text());
            });

            $('#pagebar .pagenumber').click(function () {
                submitForm($(this).text());
            });

        });
    </script>
}
//控制器中获取页面总数和分页查询方法:
private static readonly int PAGE_SIZE = 3;

        private int GetPageCount(int recordCount)//获取总页数
        {
            int pageCount = recordCount / PAGE_SIZE;
            if (recordCount % PAGE_SIZE != 0)
            {
                pageCount += 1;
            }
            return pageCount;
        }

        private List<Manto> GetPagedDataSource(IQueryable<Manto> students,
        int pageIndex, int recordCount)//分页查询获取数据库数据信息
        {
            var pageCount = GetPageCount(recordCount);
            if (pageIndex >= pageCount && pageCount >= 1)
            {
                pageIndex = pageCount - 1;
            }

            return students.OrderBy(m => m.Name)
                           .Skip(pageIndex * PAGE_SIZE)
                           .Take(PAGE_SIZE)
                           .ToList();
        }

//控制器中的Index方法:
        // GET: Mantoes
        public ActionResult Index()
        {
            var mantoes = db.Mantoes as IQueryable<Manto>;
            var recordCount = mantoes.Count();
            var pageCount = GetPageCount(recordCount);

            ViewBag.PageIndex = 0;
            ViewBag.PageCount = pageCount;


            ViewBag.BrandList = GetBrandList();
            return View(GetPagedDataSource(mantoes, 0, recordCount));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(string Name,string Brand,int PageIndex)
        {

            var mantoes = db.Mantoes as IQueryable<Manto>;

            if (!string.IsNullOrEmpty(Name))
            {
                mantoes = mantoes.Where(m => m.Name.Contains(Name));
            }
            if (!string.IsNullOrEmpty(Brand.ToString()))
            {
                mantoes = mantoes.Where(m => m.Brand.ToString() == Brand);
            }


            var recordCount = mantoes.Count();
            var pageCount = GetPageCount(recordCount);
            if (PageIndex >= pageCount && pageCount >= 1)
            {
                PageIndex = pageCount - 1;
            }

            mantoes = mantoes.OrderBy(m => m.Name)
                 .Skip(PageIndex * PAGE_SIZE).Take(PAGE_SIZE);

            ViewBag.PageIndex = PageIndex;
            ViewBag.PageCount = pageCount;


            ViewBag.BrandList = GetBrandList();

            return View(mantoes.ToList());
        }

其他都正常就是点击分页数字时候没任何反应,也没有任何错误

1条回答
等我变得足够好
2楼-- · 2019-03-16 16:46

那还不就是点击按钮事件没触发啦

查看更多
登录 后发表回答