如何将iPagedlist分页功能与ViewModel一起使用,然后加入ASP .NET MVC



我是ASP .NET MVC的新手,并且我正在尝试使用ViewModel实现Ipagedlist分页功能并加入。分页和搜索在普通页面上工作正常,但我无法使用ViewModel和Joins。

我正在遵循这种方法:http://www.asp.net/mvc/overview/getting-started/getting-with-with-with-ef-usis-mvc/sorting-filtering-filtering-filtering-ind-paging-with-with-with-with-the-the-the-the-the-the-the-the-the-the-the-the-the-the-the-t------------------> - 实体 - 框架中的AN-ASP-NET-MVC-application

这是我的ViewModel类:

 public class SponserDisplayViewModel
    {
        public Sponser Sponser { get; set; }
        public SponserDetail SponserDetail { get; set; }
        public SponserType SponserType { get; set; }
    } //--- Here All three are different classes.

我在控制器中尝试的内容:

public ActionResult Index(string searchString, int? page, string btnSearch)
        {
            var viewModel = from s in db.Sponsers
                            join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
                            from st in st2.DefaultIfEmpty()
                            select new SponserDisplayViewModel { Sponser = s, SponserType = st };
            if (btnSearch == "Reset")
            { searchString = string.Empty; }
            if (!String.IsNullOrEmpty(searchString))
            {
                viewModel = from s in db.Sponsers
                            join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
                            from st in st2.DefaultIfEmpty()
                            where st.Name.Contains(searchString)
                            select new SponserDisplayViewModel { Sponser = s, SponserType = st };
            }
            int pageSize = 20;
            int pageIndex = 1;
            pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;

//==================Getting error here
            IPagedList<SponserDisplayViewModel> po = from s in db.Sponsers
                                            join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
                                            from st in st2.DefaultIfEmpty().OrderBy(a => a.DisplayOrder).ToPagedList(pageIndex, pageSize)
                                            select new SponserDisplayViewModel { Sponser = s, SponserType = st };
            return View(po);
        }

请建议

得到了我的答案:

var po = from s in db.Sponsers
         join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
         from st in st2.DefaultIfEmpty
         orderby st.DisplayOrder
         select new SponserDisplayViewModel { Sponser = s, SponserType = st };
return View(po.ToPagedList(pageIndex, pageSize));

它为我工作

int pagesize = 7, pageindex = 1;
        pageindex = page.HasValue ? Convert.ToInt32(page) : 1;
        var list = db.tbl_Category.Where(x => x.C_Status == 1).ToList();
        IPagedList<tbl_Category> showlis = list.ToPagedList(pageindex, pagesize);

相关内容

最新更新