MVC 模型过滤

通用返回模型

/// <summary>
    /// WebApi返回数据的泛型类
    /// </summary>
    /// <typeparam name="T"></typeparam>

    public partial class VMResult<T>
    {
        public VMResult()
        {
            if (typeof(T).IsClass && typeof(T) != typeof(string))
            {
                Data = System.Activator.CreateInstance<T>();
            }
        }
        public VMResult(int pErrCode, string pResultMsg, T pData)
        {
            ErrCode = pErrCode;
            ResultMsg = pResultMsg;
            Data = pData;
        }
        int _ErrCode;
        /// <summary>
        /// 0成功,非0错误
        /// </summary>
        public int ErrCode
        {
            get { return _ErrCode ;}
            set { _ErrCode = value; }
        }
        string _ResultMsg;
        /// <summary>
        /// 文字描述信息
        /// </summary>
        public String ResultMsg { get { return _ResultMsg; } set { _ResultMsg = value; } }
        /// <summary>
        /// 泛型自定义数据
        /// </summary>
        T _Data;
        public T Data { get { return _Data; } set { _Data = value; } }

    }
View Code

模型验证:

var result = new VMResult<string>();
            if (!ModelState.IsValid)
            {
                result.ResultMsg = string.Join(",", ModelState.Values.SelectMany(m => m.Errors).Select(m => m.ErrorMessage));
                result.ErrCode = 1;
                return result;
            }
View Code

 

 

 

 

 

https://www.cnblogs.com/Leo_wl/p/6798276.html

 

posted @ 2018-06-11 14:03  bxzjzg  阅读(189)  评论(0编辑  收藏  举报