ASP.NET MVC5网站开发显示文章列表(九)
程序员文章站
2024-02-16 13:42:16
*惯,先上个效果图:
1、在ibll
在interfacecommonmodelservice接口中添加获取公共模型列表的方法
首先排序方法
//...
*惯,先上个效果图:
1、在ibll
在interfacecommonmodelservice接口中添加获取公共模型列表的方法
首先排序方法
/// <summary> /// 排序 /// </summary> /// <param name="entitys">数据实体集</param> /// <param name="rodercode">排序代码[默认:id降序]</param> /// <returns></returns> iqueryable<commonmodel> order(iqueryable<commonmodel> entitys, int rodercode); 查询数据方法 /// <summary> /// 查询分页数据列表 /// </summary> /// <param name="totalrecord">总记录数</param> /// <param name="model">模型【all全部】</param> /// <param name="pageindex">页码</param> /// <param name="pagesize">每页记录数</param> /// <param name="title">标题【不使用设置空字符串】</param> /// <param name="categoryid">栏目id【不使用设0】</param> /// <param name="inputer">用户名【不使用设置空字符串】</param> /// <param name="fromdate">起始日期【可为null】</param> /// <param name="todate">截止日期【可为null】</param> /// <param name="ordercode">排序码</param> /// <returns>分页数据列表</returns> iqueryable<commonmodel> findpagelist(out int totalrecord, int pageindex, int pagesize, string model, string title, int categoryid, string inputer, nullable<datetime> fromdate, nullable<datetime> todate, int ordercode);
2、bll
在commonmodelservice写方法实现代码,内容都很简单主要是思路,直接上代码 public iqueryable<commonmodel> findpagelist(out int totalrecord, int pageindex, int pagesize, string model, string title, int categoryid, string inputer, nullable<datetime> fromdate, nullable<datetime> todate, int ordercode) { //获取实体列表 iqueryable<commonmodel> _commonmodels = currentrepository.entities; if (model == null || model != "all") _commonmodels = _commonmodels.where(cm => cm.model == model); if (!string.isnullorempty(title)) _commonmodels = _commonmodels.where(cm => cm.title.contains(title)); if (categoryid > 0) _commonmodels = _commonmodels.where(cm => cm.categoryid == categoryid); if (!string.isnullorempty(inputer)) _commonmodels = _commonmodels.where(cm => cm.inputer == inputer); if (fromdate != null) _commonmodels = _commonmodels.where(cm => cm.releasedate >= fromdate); if (todate != null) _commonmodels = _commonmodels.where(cm => cm.releasedate <= todate); _commonmodels = order(_commonmodels, ordercode); totalrecord = _commonmodels.count(); return pagelist(_commonmodels, pageindex, pagesize).asqueryable(); } public iqueryable<commonmodel> order(iqueryable<commonmodel> entitys, int ordercode) { switch(ordercode) { //默认排序 default: entitys = entitys.orderbydescending(cm => cm.releasedate); break; } return entitys; }
3、web
由于commonmodel跟我们前台显示的数据并不一致,为了照顾datagrid中的数据显示再在ninesky.web.models中再构造一个视图模型commonmodelviewmodel
using system; namespace ninesky.web.models { /// <summary> /// commonmodel视图模型 /// <remarks> /// 创建:2014.03.10 /// </remarks> /// </summary> public class commonmodelviewmodel { public int modelid { get; set; } /// <summary> /// 栏目id /// </summary> public int categoryid { get; set; } /// <summary> /// 栏目名称 /// </summary> public string categoryname { get; set; } /// <summary> /// 模型名称 /// </summary> public string model { get; set; } /// <summary> /// 标题 /// </summary> public string title { get; set; } /// <summary> /// 录入者 /// </summary> public string inputer { get; set; } /// <summary> /// 点击 /// </summary> public int hits { get; set; } /// <summary> /// 发布日期 /// </summary> public datetime releasedate { get; set; } /// <summary> /// 状态 /// </summary> public int status { get; set; } /// <summary> /// 状态文字 /// </summary> public string statusstring { get { return ninesky.models.commonmodel.statuslist[status]; } } /// <summary> /// 首页图片 /// </summary> public string defaultpicurl { get; set; } } }
在articlecontroller中添加一个返回json类型的jsonlist方法
/// <summary> /// 文章列表json【注意权限问题,普通人员是否可以访问?】 /// </summary> /// <param name="title">标题</param> /// <param name="input">录入</param> /// <param name="category">栏目</param> /// <param name="fromdate">日期起</param> /// <param name="todate">日期止</param> /// <param name="pageindex">页码</param> /// <param name="pagesize">每页记录</param> /// <returns></returns> public actionresult jsonlist(string title, string input, nullable<int> category, nullable<datetime> fromdate, nullable<datetime> todate, int pageindex = 1, int pagesize = 20) { if (category == null) category = 0; int _total; var _rows = commonmodelservice.findpagelist(out _total, pageindex, pagesize, "article", title, (int)category, input, fromdate, todate, 0).select( cm => new ninesky.web.models.commonmodelviewmodel() { categoryid = cm.categoryid, categoryname = cm.category.name, defaultpicurl = cm.defaultpicurl, hits = cm.hits, inputer = cm.inputer, model = cm.model, modelid = cm.modelid, releasedate = cm.releasedate, status = cm.status, title = cm.title }); return json(new { total = _total, rows = _rows.tolist() }); }
下面是做界面了,在添加 list方法,这里不提供任何数据,数据在jsonlist 中获得
/// <summary> /// 全部文章 /// </summary> /// <returns></returns> public actionresult list() { return view(); }
右键添加视图
<div id="toolbar"> <div> <a href="#" class="easyui-linkbutton" data-options="iconcls:'icon-edit',plain:true" >修改</a> <a href="#" class="easyui-linkbutton" data-options="iconcls:'icon-remove',plain:true" ">删除</a> <a href="#" class="easyui-linkbutton" data-options="iconcls:'icon-reload',plain:true" onclick="$('#article_list').datagrid('reload');">刷新</a> </div> <div class="form-inline"> <label>栏目</label><input id="combo_category" data-options="url:'@url.action("jsontree", "category", new { model="article" })'" class="easyui-combotree" /> <label>标题</label> <input id="textbox_title" class="input-easyui" style="width:280px" /> <label>录入人</label><input id="textbox_inputer" class="input-easyui" /> <label>添加日期</label> <input id="datebox_fromdate" type="datetime" class="easyui-datebox" style="width:120px" /> - <input id="datebox_todate" type="datetime" class="easyui-datebox" style="width:120px; " /> <a href="#" id="btn_search" data-options="iconcls:'icon-search'" class="easyui-linkbutton">查询</a> </div> </div> <table id="article_list"></table> <script src="~/scripts/common.js"></script> <script type="text/javascript"> $("#article_list").datagrid({ loadmsg: '加载中……', pagination:true, url: '@url.action("jsonlist","article")', columns: [[ { field: 'modelid', title: 'id', checkbox: true }, { field: 'categoryname', title: '栏目'}, { field: 'title', title: '标题'}, { field: 'inputer', title: '录入', align: 'right' }, { field: 'hits', title: '点击', align: 'right' }, { field: 'releasedate', title: '发布日期', align: 'right', formatter: function (value, row, index) { return jsondateformat(value); } }, { field: 'statusstring', title: '状态', width: 100, align: 'right' } ]], toolbar: '#toolbar', idfield: 'modelid', }); //查找 $("#btn_search").click(function () { $("#article_list").datagrid('load', { title: $("#textbox_title").val(), input: $("#textbox_inputer").val(), category: $("#combo_category").combotree('getvalue'), fromdate: $("#datebox_fromdate").datebox('getvalue'), todate: $("#datebox_todate").datebox('getvalue') }); }); } </script>
上面都是easyui-datagrid的内容。
总体思路是bll中实现查询公共模型列表,web中添加一个jsonlist方法调用bll中的方法并返回列表的json类型。然后再添加一个list调用jsonlist用来显示。下篇文章做删除和修改操作,希望大家会持续关注。