今天積分系統MPM (Merit Points Management) 完成。javascript
asp.net MVC4.0 EF DBFirst EasyUI 登錄頁面採用bootstrap。(仍是bootstrap好看些!)html
功能不多,就一個模型層,和一個 webUI 展示層。java
權限判斷採用 過濾器,每個Action都會通過過濾器。web
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(new MyActionFilterAttribute()); } /// <summary> /// 標記爲NoFilterAttribute 屬性的的方法,過濾器在這裏撲捉到,而後 直接 return,不作權限管控 /// </summary> [AttributeUsage(AttributeTargets.Method)] public class NoFilterAttribute : Attribute { } /// <summary> /// Action方法 過濾器 類 /// </summary> public class MyActionFilterAttribute : ActionFilterAttribute { /// <summary> /// 在 Action方法以前 調用 /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { string strController = filterContext.RouteData.Values["controller"].ToString(); if (strController == "Login" || strController == "Home") { return; } object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoFilterAttribute), true); if (attrs.Length > 0) return; HttpSessionStateBase session = filterContext.HttpContext.Session; if (session != null) { var activeSession = session["KT.MPM.web"]; if (activeSession == null) { filterContext.HttpContext.Response.Redirect("/Login"); return; } var userWorkNum = activeSession.ToString(); var mpmDb = new MPMEntities(); if (mpmDb.SysPower.FirstOrDefault(x => x.UserWorkNum == userWorkNum) == null) { filterContext.HttpContext.Response.Redirect("/Login"); } } base.OnActionExecuting(filterContext); } } }
這樣作呢,仍是比較消性能的。ajax
UI 框架的搭建 沒有 採用 Easyui 官方 documentation 頁面的那種,不採用Iframe,直接 ajax 加載 子頁面。這樣速度確實很快。缺點就是很差調試。json
我使用了Iframebootstrap
主頁面代碼以下:session
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <style> .treegrid-tr-tree td { border-style: none; } .datagrid-btable td { border-style: none; } </style> <body class="easyui-layout" style="text-align: left"> <div region="west" split="true" title="菜單" style="width: 250px; padding: 5px;"> <table id="MenuTree" style="width:100%" title="" class="easyui-treegrid" data-options=" method:'post', url:'@Url.Action("Index")',onClickRow:fun, treeField:'MenuName', fixed:true, idField:'MenuID' "> <thead> <tr> <th data-options="field:'MenuName',width:230,formatter:formatUrl"></th> </tr> </thead> </table> <script> function fun(row) { addTab(row.MenuName, row.Url); } function formatUrl(val, row) { if (row.Url != "") { return '<span class="e-link" href="javascript:void(0)" onclick="addTab(' + "'" + row.MenuName + "'" + ',' + "'" + row.Url + "'" + ')">' + row.MenuName + '</span>'; } else { return '<a class="e-link" href="javascript:void(0)">' + row.MenuName + '</a>'; } } $(document).ready(function () { }); </script> </div> <div region="center"> <div id="tt" class="easyui-tabs" fit="true" border="false" plain="true"> </div> </div> <script> function addTab(title, url) { if ($('#tt').tabs('exists', title)) { $('#tt').tabs('select', title); } else { var content = '<iframe scrolling="auto" frameborder="0" src="' + url + '" style="width:100%;height:99%;"></iframe>'; $('#tt').tabs('add', { title: title, content: content, closable: true }); } } function showMsgTopCenter(title, msg) { $.messager.show({ title: title, msg: msg, showType: 'null', showSpeed: 0, style: { top: document.body.scrollTop + document.documentElement.scrollTop, timeout: 1000 } }); } </script>
easyui 請求的數據都是 JSON 格式,因此項目中 大量用到了 json.net
好比一個有頁腳的json :app
public string UserOtherRecordForOther(UserOtherRecordData model) { var dataGridJson = new DataGridJson(); var data = _mpmDb.view_UserOtherRecordForOther.Where(x => x.ObjectUser == model.RecordUser).Where( y => y.CreateDate == model.CreateDate).ToList(); dataGridJson.rows = data; dataGridJson.footer = new List<view_UserOtherRecordForOther>() { new view_UserOtherRecordForOther() { AllPoint=data.Sum(x=>x.AllPoint), OutputValue=data.Sum(x=>x.OutputValue) } }; return JsonConvert.SerializeObject(dataGridJson); } public class DataGridJson { public static int i = 10; public int total { get; set; } //記錄的總條數 public object rows { get; set; } //具體內容 public object footer { get; set; } //具體內容 }
裏面必須再包一層List.框架
使用到了一個擴展方法 來獲取 當前登錄用戶的工號。
namespace KT.MPM.web.Controllers { public static class Expend { public static string GetUserWorkNum(this Controller controller) { return controller.Session["KT.MPM.web"].ToString(); } } }
也有人說,直接 寫一個類,繼承 Controller 類不就行了嘛。
還有一個Excel 導出功能。
使用的是 NPOI 。
function ToExcel() { location.href = '@Url.Action("ExportExcel", "EveryDayPointReport")' + '?createDate=' + CreateDate_Temp; }
[FilterConfig.NoFilterAttribute] public ActionResult ExportExcel(DateTime? createDate) { String newfileName = DateTime.Now.ToString("yyyyMMddHHmmssff"); var list = Search(createDate); var fileOne = new FileStream(Server.MapPath("/template/EveryDayReport.xls"), FileMode.Open, FileAccess.ReadWrite); var wbOne = new HSSFWorkbook(fileOne); var sheet = (HSSFSheet)wbOne.GetSheetAt(0); int startRow = 2; foreach (var item in list) { var rowOne = (HSSFRow)sheet.CreateRow(startRow); rowOne.CreateCell(0).SetCellValue(item.UserWorkNum); rowOne.CreateCell(1).SetCellValue(item.UserName); 。。。。。。 startRow = startRow + 1; } var ms = new MemoryStream(); wbOne.Write(ms); return File(ms.ToArray(), "application/vnd.ms-excel", HttpUtility.UrlEncode(string.Format("{0}.xls", newfileName))); }
MVC就這點好,這樣寫,永遠不會啓動迅雷以後下載到錯誤的頁面。之前用aspx 頁面裏面的導出功能,直接就把aspx頁面給下載過來了。必須用通常處理程序作。
每次 dataGrid 查詢,只不過是 輕量級 的發送 輕量級 的送回來(只須要更改datagrid 的url 參數)。服務端 客戶端 都消耗 比較少。