龙空技术网

NET+EF+MVC接口开发输出分页的数据列表

娱乐一分哥 156

前言:

此时朋友们对“net20list分页”都比较关切,你们都需要学习一些“net20list分页”的相关文章。那么小编在网摘上汇集了一些对于“net20list分页””的相关文章,希望朋友们能喜欢,兄弟们一起来学习一下吧!

在NET+EF+MVC接口开发的时候,如何 输出带 分页的数据列表页,以下是完整方法,可以在开发时借鉴思路。

[HttpGet]

public HttpResponseMessage Get_VideoPageList(decimal ParentID, decimal ParentloginID, decimal NewEnrolmentID, Nullable<int> sort = 0, Nullable<decimal> knowledgetypeID = 0, Nullable<decimal> knowledgeageID = 0, decimal KindergartenID = 0, decimal TrialID = 0, int pageIndex = 1, int pageSize = 10, string Token = null)

{

HttpResponseMessage result = null;

var ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"参数有误\",\"data\":{}}";

if (TrialID <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"总部试用ID有误\",\"data\":{}}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

else if (KindergartenID <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"ID有误\",\"data\":{}}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

else if (ParentID <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"一级分类ID有误\",\"data\":{}}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

else if (ParentloginID <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"家长ID有误\",\"data\":{}}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

else if (NewEnrolmentID <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"宝贝ID有误\",\"data\":{}}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

else

{

#region 验证token

if (!string.IsNullOrEmpty(Token))

{

int t = new WisdomStars.Bll.Api.Common().PUB_VerificationToken(Token);

if (t <= 0)

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"登录失效\",\"data\":{}}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

}

else

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.失败 + "\",\"msg\":\"token不能为空\",\"data\":{}}";

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

#endregion 验证token

ef.Configuration.ProxyCreationEnabled = false;

Expression<Func<knowledge, bool>> where = PredicateExtensionses.True<knowledge>();

where = where.And(a => a.isdeleted == 0 && a.knowledgetype.ParentID == ParentID);

if (knowledgetypeID > 0)

{

where = where.And(a => a.knowledgetypeID == knowledgetypeID);

}

if (knowledgeageID > 0)

{

where = where.And(a => a.knowledgeageID == knowledgeageID);

}

IPagedList<hx_knowledge> list;

if (sort == 1)

{

//按访问量倒序

list = ef.hx_knowledge.Where(where).OrderByDescending(p => p.hit).AsNoTracking().ToPagedList(pageIndex, pageSize);

}

else

{

//按日期倒序

list = ef.knowledge.Where(where).OrderByDescending(p => p.createtime).AsNoTracking().ToPagedList(pageIndex, pageSize);

}

int TotalItemCount = list.TotalItemCount; //总数量

int TotalPageCount = 0;

if (TotalItemCount % pageSize == 0)

{

TotalPageCount = TotalItemCount / pageSize; //总页数

}

else

{

TotalPageCount = TotalItemCount / pageSize + 1; //总页数

}

List<Api_knowledge> knowledgelist = new List<Api_knowledge>();

if (list != null && list.Count() > 0)

{

foreach (var item in list)

{

Api_knowledge info = new Api_knowledge();

info.knowledgeID = item.knowledgeID;

info.knowledgename = item.knowledgename;

if (!string.IsNullOrEmpty(item.descript))

{

info.descript = item.descript.ToString();

}

else

{

info.descript = "";

}

if (!string.IsNullOrEmpty(item.photo))

{

info.photo = System.Web.Configuration.WebConfigurationManager.AppSettings["ImagesBrowseServer"].ToString() + item.photo;

}

else

{

info.photo = "";

}

info.hit = item.hit;

knowledgelist.Add(info);

}

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.成功 + "\",\"msg\":\"获取成功\",\"data\":{\"TotalPageCount\":" + TotalPageCount + ",\"TotalItemCount\":" + TotalItemCount + ",\"pageIndex\":\"" + pageIndex + "\",\"pageSize\":\"" + pageSize + "\",\"list\":" + knowledgelist.ToEnJsonString() + "}}";

}

else

{

ReturnStr = "{\"result\":\"" + (int)KeyEnum.AppReturn.无数据 + "\",\"msg\":\"无数据\",\"data\":{}}";

}

}

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

以上是NET+EF+MVC开发接口中常用输出分页的数据列表方法,欢迎交流!

标签: #net20list分页