/// <summary> /// success status /// </summary> /// <param name="controller">控制器</param> /// <returns>jsonResult</returns> public static Microsoft.AspNetCore.Mvc.JsonResult JsonSuccessStatus(this Microsoft.AspNetCore.Mvc.Controller controller) { return(controller.Json(new JsonResponse() { StatusCode = ResponseStatus.请求成功, Message = ResponseStatus.请求成功.ToString() }, defaultJSS)); }
/// <summary> /// 回傳遮蔽後的JSON結果 /// </summary> /// <typeparam name="T">資料類型</typeparam> /// <param name="controller">控制器實例</param> /// <param name="data">資料</param> /// <param name="patternName">模式名稱</param> /// <param name="jsonSerializerSettings">JSON序列化設定</param> /// <returns>遮蔽後的JSON結果</returns> public static JsonResult JsonByMask <T>( this Controller controller, T data, string patternName = null, JsonSerializerSettings jsonSerializerSettings = null) { // 檢查是否有自訂序列化設定選項 if (jsonSerializerSettings == null) { // 直接調用預設的設定序列化 return(controller.Json( controller.Mask(data, patternName))); } else { // 加入自訂序列化設定選項 return(controller.Json( controller.Mask(data, patternName), jsonSerializerSettings)); } }
public static JsonResult BuildDataTableForm <T>(this Microsoft.AspNetCore.Mvc.Controller controller, List <T> data) { // Initialization. string search = controller.Request.Form["search[value]"][0]; string draw = controller.Request.Form["draw"][0]; string order = controller.Request.Form["order[0][column]"][0]; string orderDir = controller.Request.Form["order[0][dir]"][0]; int startRec = Convert.ToInt32(controller.Request.Form["start"][0]); int pageSize = Convert.ToInt32(controller.Request.Form["length"][0]); // Total record count. int totalRecords = data.Count; // Verification. if (!string.IsNullOrEmpty(search) && !string.IsNullOrWhiteSpace(search)) { // Apply search var tempData = new List <T>(); foreach (var item in typeof(T).GetProperties()) { if (item.PropertyType.Name != typeof(List <>).Name) { tempData.AddRange(data.Where(m => { return(m.GetType().GetProperty(item.Name).GetValue(m, null).ToString().ToLower().Contains(search.ToLower())); })); } } data = tempData.Distinct().ToList(); } // Sorting. var orderField = controller.Request.Form["columns[" + order + "][data]"][0]; data = SortByColumnWithOrder(orderField, orderDir, data); // Filter record count. int recFilter = data.Count; // Apply pagination. data = data.Skip(startRec).Take(pageSize).ToList(); var finalData = new List <Dictionary <string, object> >(); foreach (var item in data) { //TODO: When you add a new data formatting here, make sure to add also in dataTable.js Ln 24 var expObj = new Dictionary <string, object>(); foreach (var p in item.GetType().GetProperties()) { expObj.Add(p.Name.ToCamelCase(), p.GetValue(item)); if (p.PropertyType.BaseType == typeof(Enum)) { expObj.Add(p.Name.ToCamelCase() + "Desc", p.GetValue(item).ToString()); } else if (p.PropertyType == typeof(DateTimeOffset)) { expObj.Add(p.Name.ToCamelCase() + "Desc", DateTime.Parse(p.GetValue(item).ToString()).ToString("MMM d, yyyy HH:mm:ss")); } } finalData.Add(expObj); } // Loading drop down lists. var result = controller.Json(new { draw = Convert.ToInt32(draw), recordsTotal = totalRecords, recordsFiltered = recFilter, data = finalData }); return(result); }