mvc3 easyui 異常協助類 (封裝 try catch操作和統一調用 增刪改查 方法) 和 easy ui 協助類

來源:互聯網
上載者:User

using System;using System.Collections.Generic;using System.Linq;using System.Text;using log4net.Core;using System.Linq.Expressions;using System.Web.Mvc;using System.Web.Script.Serialization;using System.Text.RegularExpressions;namespace Model.Common{    /// <summary>    /// 異常協助類 封裝 try catch操作和統一調用 增刪改 方法    /// </summary>    public class ExceptionHelper    {        static log4net.ILog log=null;        static JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();//json轉換類               public ExceptionHelper(string classname)        {            log = log4net.LogManager.GetLogger(classname);        }        /// <summary>        /// 無傳回值無參數的方法調用        /// </summary>        /// <param name="action"></param>        public  void Invoke(Action action)        {            try            {                action();            }            catch (Exception ex)            {                log.Error(ex.Message);            }        }        /// <summary>        /// 有傳回值無參數的方法調用        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="func"></param>        /// <returns></returns>        public static T Invoke<T>(Func<T> func)        {            try            {                return func();            }            catch (Exception ex)            {                 log.Error(ex.Message);                 return default(T);            }        }        /// <summary>        /// 公用的擷取非查詢執行結果的方法        /// </summary>        /// <param name="op">操作方法</param>        /// <param name="nu">newuser對象</param>        /// <param name="errormsg">錯誤資訊</param>        /// <returns>json結果</returns>        public static string  GetNoQueryJsonResult<T>(Func<T,int> op, T nu, string errormsg,bool isvalid)        {            EasyResult er = new EasyResult();            er.success = false;            try            {                if (isvalid)                {                    int i = op(nu);                    if (i > 0)                    {                        er.success = true;                    }                    else                    {                        er.errorMsg = errormsg;                    }                }                else                {                    er.errorMsg = "驗證失敗,請檢查資料";                }            }            catch (Exception ex)            {                er.errorMsg = ex.Message;                log.Error(ex.Message,ex);            }            return JsonSerializer.Serialize(er);        }    }    /// <summary>    /// 泛型 異常協助類  封裝 try catch操作和統一調用 查詢 方法    /// </summary>    /// <typeparam name="ModelType"></typeparam>    public class ExceptionHelper<ModelType>    {        public delegate List<ModelType> FuncEx(int t1, int t2, Dictionary<string, string> t3, out int t4);        static log4net.ILog log = null;        static JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();//json轉換類        public ExceptionHelper(string classname)        {            log = log4net.LogManager.GetLogger(classname);        }        /// <summary>        /// 公用的擷取查詢執行結果的方法(分頁)        /// </summary>        /// <param name="op">操作方法</param>        /// <param name="pageIndex">頁碼</param>        /// <param name="pageSize">每頁記錄數</param>        /// <param name="dicCondition">查詢條件</param>        /// <param name="totalcount">總數</param>        /// <returns>json資料集合</returns>        public static string GetQueryJsonResult(FuncEx op, int pageIndex, int pageSize, Dictionary<string, string> dicCondition, out int totalcount)        {            totalcount = 0;            try            {                List<ModelType> lsnu = op(pageIndex, pageSize, dicCondition, out totalcount);                EasyDataGrid<ModelType> edg = new EasyDataGrid<ModelType>();                edg.total = totalcount;//總記錄數                edg.rows = lsnu;//行資料                string str = JsonSerializer.Serialize(edg);                str = Regex.Replace(str, @"\\/Date\((\d+)\)\\/", match =>                {                    DateTime dt = new DateTime(1970, 1, 1);                    dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));                    dt = dt.ToLocalTime();                    return dt.ToString("yyyy-MM-dd");                });                return str;            }            catch (Exception ex)            {                log.Error(ex.Message, ex);            }            return "";        }    }}

easy ui 協助類

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Model.Common{    /// <summary>    ///  easyui tree  json資料類    /// </summary>    public class EasyTreeNode    {        public EasyTreeNode()        {            attributes = new Dictionary<string, string>();            children = new List<EasyTreeNode>();        }        /// <summary>        /// 樹的id        /// </summary>        public int id { get; set; }        /// <summary>        /// 樹顯示的文字        /// </summary>        public string text { get; set; }        /// <summary>        /// 其他屬性 <屬性名稱,屬性值>        /// </summary>        public Dictionary<string, string> attributes { get; set; }        /// <summary>        /// 子節點        /// </summary>        public List<EasyTreeNode> children { get; set; }    }    /// <summary>    /// easyui datagrid json 資料類    /// </summary>    public class EasyDataGrid<T>    {         public EasyDataGrid()        {            rows = new List<T>();        }        /// <summary>        /// 總記錄數        /// </summary>        public int total { get; set; }        /// <summary>        /// 行資料  (<欄位名,欄位值>)的集合        /// </summary>        public List<T> rows { get; set; }    }    /// <summary>    /// 返回資訊    /// </summary>    public class EasyResult     {        public string errorMsg { get; set; }        public bool success { get; set; }    }}

需要引用相關dll.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.