【发布时间】:2016-06-23 09:55:52
【问题描述】:
我是 asp .net mvc 的新手,我正在尝试使用 viewmodel 和 join 实现 IPagedList 分页功能。对于普通页面,分页和搜索工作正常,但我无法使用视图模型和连接。
这是我的 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.
这是我在 Controller 中尝试过的:
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);
}
请推荐
【问题讨论】:
标签: asp.net-mvc