当前位置:Gxlcms > asp.net > ASP.NET MVC5网站开发显示文章列表(九)

ASP.NET MVC5网站开发显示文章列表(九)

时间:2021-07-01 10:21:17 帮助过:36人阅读

老习惯,先上个效果图:

1、在IBLL
在InterfaceCommonModelService接口中添加获取公共模型列表的方法
首先排序方法

  1. /// <summary>
  2. /// 排序
  3. /// </summary>
  4. /// <param name="entitys">数据实体集</param>
  5. /// <param name="roderCode">排序代码[默认:ID降序]</param>
  6. /// <returns></returns>
  7. IQueryable<CommonModel> Order(IQueryable<CommonModel> entitys, int roderCode);
  8. 查询数据方法
  9. /// <summary>
  10. /// 查询分页数据列表
  11. /// </summary>
  12. /// <param name="totalRecord">总记录数</param>
  13. /// <param name="model">模型【All全部】</param>
  14. /// <param name="pageIndex">页码</param>
  15. /// <param name="pageSize">每页记录数</param>
  16. /// <param name="title">标题【不使用设置空字符串】</param>
  17. /// <param name="categoryID">栏目ID【不使用设0】</param>
  18. /// <param name="inputer">用户名【不使用设置空字符串】</param>
  19. /// <param name="fromDate">起始日期【可为null】</param>
  20. /// <param name="toDate">截止日期【可为null】</param>
  21. /// <param name="orderCode">排序码</param>
  22. /// <returns>分页数据列表</returns>
  23. 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

  1. 在CommonModelService写方法实现代码,内容都很简单主要是思路,直接上代码
  2. 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)
  3. {
  4. //获取实体列表
  5. IQueryable<CommonModel> _commonModels = CurrentRepository.Entities;
  6. if (model == null || model != "All") _commonModels = _commonModels.Where(cm => cm.Model == model);
  7. if (!string.IsNullOrEmpty(title)) _commonModels = _commonModels.Where(cm => cm.Title.Contains(title));
  8. if (categoryID > 0) _commonModels = _commonModels.Where(cm => cm.CategoryID == categoryID);
  9. if (!string.IsNullOrEmpty(inputer)) _commonModels = _commonModels.Where(cm => cm.Inputer == inputer);
  10. if (fromDate != null) _commonModels = _commonModels.Where(cm => cm.ReleaseDate >= fromDate);
  11. if (toDate != null) _commonModels = _commonModels.Where(cm => cm.ReleaseDate <= toDate);
  12. _commonModels = Order(_commonModels, orderCode);
  13. totalRecord = _commonModels.Count();
  14. return PageList(_commonModels, pageIndex, pageSize).AsQueryable();
  15. }
  16. public IQueryable<CommonModel> Order(IQueryable<CommonModel> entitys, int orderCode)
  17. {
  18. switch(orderCode)
  19. {
  20. //默认排序
  21. default:
  22. entitys = entitys.OrderByDescending(cm => cm.ReleaseDate);
  23. break;
  24. }
  25. return entitys;
  26. }

3、web
由于CommonModel跟我们前台显示的数据并不一致,为了照顾datagrid中的数据显示再在Ninesky.Web.Models中再构造一个视图模型CommonModelViewModel

  1. using System;
  2. namespace Ninesky.Web.Models
  3. {
  4. /// <summary>
  5. /// CommonModel视图模型
  6. /// <remarks>
  7. /// 创建:2014.03.10
  8. /// </remarks>
  9. /// </summary>
  10. public class CommonModelViewModel
  11. {
  12. public int ModelID { get; set; }
  13. /// <summary>
  14. /// 栏目ID
  15. /// </summary>
  16. public int CategoryID { get; set; }
  17. /// <summary>
  18. /// 栏目名称
  19. /// </summary>
  20. public string CategoryName { get; set; }
  21. /// <summary>
  22. /// 模型名称
  23. /// </summary>
  24. public string Model { get; set; }
  25. /// <summary>
  26. /// 标题
  27. /// </summary>
  28. public string Title { get; set; }
  29. /// <summary>
  30. /// 录入者
  31. /// </summary>
  32. public string Inputer { get; set; }
  33. /// <summary>
  34. /// 点击
  35. /// </summary>
  36. public int Hits { get; set; }
  37. /// <summary>
  38. /// 发布日期
  39. /// </summary>
  40. public DateTime ReleaseDate { get; set; }
  41. /// <summary>
  42. /// 状态
  43. /// </summary>
  44. public int Status { get; set; }
  45. /// <summary>
  46. /// 状态文字
  47. /// </summary>
  48. public string StatusString { get { return Ninesky.Models.CommonModel.StatusList[Status]; } }
  49. /// <summary>
  50. /// 首页图片
  51. /// </summary>
  52. public string DefaultPicUrl { get; set; }
  53. }
  54. }

在ArticleController中添加一个返回json类型的JsonList方法

  1. /// <summary>
  2. /// 文章列表Json【注意权限问题,普通人员是否可以访问?】
  3. /// </summary>
  4. /// <param name="title">标题</param>
  5. /// <param name="input">录入</param>
  6. /// <param name="category">栏目</param>
  7. /// <param name="fromDate">日期起</param>
  8. /// <param name="toDate">日期止</param>
  9. /// <param name="pageIndex">页码</param>
  10. /// <param name="pageSize">每页记录</param>
  11. /// <returns></returns>
  12. public ActionResult JsonList(string title, string input, Nullable<int> category, Nullable<DateTime> fromDate, Nullable<DateTime> toDate, int pageIndex = 1, int pageSize = 20)
  13. {
  14. if (category == null) category = 0;
  15. int _total;
  16. var _rows = commonModelService.FindPageList(out _total, pageIndex, pageSize, "Article", title, (int)category, input, fromDate, toDate, 0).Select(
  17. cm => new Ninesky.Web.Models.CommonModelViewModel()
  18. {
  19. CategoryID = cm.CategoryID,
  20. CategoryName = cm.Category.Name,
  21. DefaultPicUrl = cm.DefaultPicUrl,
  22. Hits = cm.Hits,
  23. Inputer = cm.Inputer,
  24. Model = cm.Model,
  25. ModelID = cm.ModelID,
  26. ReleaseDate = cm.ReleaseDate,
  27. Status = cm.Status,
  28. Title = cm.Title
  29. });
  30. return Json(new { total = _total, rows = _rows.ToList() });
  31. }

下面是做界面了,在添加 List方法,这里不提供任何数据,数据在JsonList 中获得

  1. /// <summary>
  2. /// 全部文章
  3. /// </summary>
  4. /// <returns></returns>
  5. public ActionResult List()
  6. {
  7. return View();
  8. }

右键添加视图

  1. <div id="toolbar">
  2. <div>
  3. <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" >修改</a>
  4. <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" ">删除</a>
  5. <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="$('#article_list').datagrid('reload');">刷新</a>
  6. </div>
  7. <div class="form-inline">
  8. <label>栏目</label><input id="combo_category" data-options="url:'@Url.Action("JsonTree", "Category", new { model="Article" })'" class="easyui-combotree" />
  9. <label>标题</label> <input id="textbox_title" class="input-easyui" style="width:280px" />
  10. <label>录入人</label><input id="textbox_inputer" class="input-easyui" />
  11. <label>添加日期</label>
  12. <input id="datebox_fromdate" type="datetime" class="easyui-datebox" style="width:120px" /> -
  13. <input id="datebox_todate" type="datetime" class="easyui-datebox" style="width:120px; " />
  14. <a href="#" id="btn_search" data-options="iconCls:'icon-search'" class="easyui-linkbutton">查询</a>
  15. </div>
  16. </div>
  17. <table id="article_list"></table>
  18. <script src="~/Scripts/Common.js"></script>
  19. <script type="text/javascript">
  20. $("#article_list").datagrid({
  21. loadMsg: '加载中……',
  22. pagination:true,
  23. url: '@Url.Action("JsonList","Article")',
  24. columns: [[
  25. { field: 'ModelID', title: 'ID', checkbox: true },
  26. { field: 'CategoryName', title: '栏目'},
  27. { field: 'Title', title: '标题'},
  28. { field: 'Inputer', title: '录入', align: 'right' },
  29. { field: 'Hits', title: '点击', align: 'right' },
  30. { field: 'ReleaseDate', title: '发布日期', align: 'right', formatter: function (value, row, index) { return jsonDateFormat(value); } },
  31. { field: 'StatusString', title: '状态', width: 100, align: 'right' }
  32. ]],
  33. toolbar: '#toolbar',
  34. idField: 'ModelID',
  35. });
  36. //查找
  37. $("#btn_search").click(function () {
  38. $("#article_list").datagrid('load', {
  39. title: $("#textbox_title").val(),
  40. input: $("#textbox_inputer").val(),
  41. category: $("#combo_category").combotree('getValue'),
  42. fromDate: $("#datebox_fromdate").datebox('getValue'),
  43. toDate: $("#datebox_todate").datebox('getValue')
  44. });
  45. });
  46. }
  47. </script>

上面都是easyui-datagrid的内容。
总体思路是BLL中实现查询公共模型列表,web中添加一个JsonList方法调用BLL中的方法并返回列表的Json类型。然后再添加一个List调用JsonList用来显示。下篇文章做删除和修改操作,希望大家会持续关注。

人气教程排行